Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 16, 2022 07:01 pm GMT

Benchmark: Prisma VS TypeORM

Prisma and TypeORM are possibly the main ORM choices for the JavaScript ecosystem in 2022, so I have decided to do a performance benchmark comparing these two tools.

Notes

This is a very simple benchmark. For this I just used console.time and Excel. If there is anything wrong or uncovered, please let me know.

Benchmark setup

How this benchmark was made? Which tools were used? Which database? I want to be 100% transparent in this benchmark, so I will explain every detail of how this was implemented.

Tools used

Database

I used a PostgreSQL image running inside docker containers. To have as little interference as possible between one benchmark and another, I used two databases, one for each, and in separate containers.

Test environment

Notebook Dell Inspiron 15:

  • 16GB RAM
  • Intel 7 11th gen 5.0GHz
  • 512GB SSD
  • NVIDIA GeForce MX450, 2GB GDDR5
  • Kubuntu 22.04 LTS

Approach

The benchmark was divided in two different files: prisma-benchmark.ts and typeorm-benchmark.ts, and you have to measure the performance one at a time. Doing the benchmark by this way, you minimize external factors, like two simmultaneous connections to the database, garbage collector and CPU usage.

Benchmark code

This benchmark uses 2 different databases, but with the exact same tables: User and User_Address.

Create many Users

Code for "Create many users" in prisma-benchmark.ts:

const createManyUsers = async (count: number) => {  const fCount = count.toLocaleString('en-US')  const fakeUsers = Array.from({ length: count }, () => ({    name: faker.name.fullName(),    email: faker.internet.email(),    password: faker.internet.password(),    group: userGroups[Math.floor(Math.random() * userGroups.length)]  }))  console.time(`Create(many) ${fCount} users - PRISMA`)  await prisma.user.createMany({    data: fakeUsers,  })  console.timeEnd(`Create(many) ${fCount} users - PRISMA`)}

Code for "Create many users" in typeorm-benchmark.ts:

const createManyUsers = async (count: number) => {  const fCount = count.toLocaleString('en-US')  const fakeUsers = Array.from({ length: count }, () => ({    name: faker.name.fullName(),    email: faker.internet.email(),    password: faker.internet.password(),    group: userGroups[Math.floor(Math.random() * userGroups.length)]  }))  console.time(`Create(many) ${fCount} users - TYPEORM`)  await userRepository.save(fakeUsers)  console.timeEnd(`Create(many) ${fCount} users - TYPEORM`)}

Find all users

Code for "Find all users" in prisma-benchmark.ts:

const findUsers = async () => {  console.time('Find users - PRISMA')  await prisma.user.findMany()  console.timeEnd('Find users - PRISMA')}

Code for "Find all users" in typeorm-benchmark.ts:

const findUsers = async () => {  console.time('Find users - TYPEORM')  await userRepository.find()  console.timeEnd('Find users - TYPEORM')}

Find users that match a given condition

Code for "Find users that match a given condition" in prisma-benchmark.ts:

const findByGroup = async () => {  console.time('Find users by group - PRISMA')  await prisma.user.findMany({    where: {      group: 'guest'    },  })  console.timeEnd('Find users by group - PRISMA')}

Code for "Find users that match a given condition" in typeorm-benchmark.ts:

const findByGroup = async () => {  console.time('Find users by group - TYPEORM')  await userRepository.find({    where: {      group: 'guest'    },  })  console.timeEnd('Find users by group - TYPEORM')}

Creating users in a stress scenario

Code for "Creating users in a stress scenario" in prisma-benchmark.ts:

const createUsersIntensive = async (count: number) => {  const fakeUserAddresses = Array.from({ length: count }, () => ({    address: faker.address.streetAddress(),    city: faker.address.city(),    state: faker.address.state(),    zip: faker.address.zipCode(),    country: faker.address.country(),  }))  const fakeUsers = Array.from({ length: count }, () => ({    name: faker.name.fullName(),    email: faker.internet.email(),    password: faker.internet.password(),    group: userGroups[Math.floor(Math.random() * userGroups.length)],    userAddresses: fakeUserAddresses  }))  console.time(`Create users intensive - PRISMA`)  for (const user of fakeUsers) {    await prisma.user.create({      data: {        ...user,        userAddresses: {          create: user.userAddresses        }      },    })  }  console.timeEnd(`Create users intensive - PRISMA`)}

Code for "Creating users in a stress scenario" in typeorm-benchmark.ts:

const createUsersIntensive = async (count: number) => {  const fakeUserAddresses = Array.from({ length: count }, () => ({    address: faker.address.streetAddress(),    city: faker.address.city(),    state: faker.address.state(),    zip: faker.address.zipCode(),    country: faker.address.country(),  }))  const fakeUsers = Array.from({ length: count }, () => ({    name: faker.name.fullName(),    email: faker.internet.email(),    password: faker.internet.password(),    group: userGroups[Math.floor(Math.random() * userGroups.length)],    userAddresses: fakeUserAddresses  }))  console.time(`Create users intensive - TYPEORM`)  for (const user of fakeUsers) {    await userRepository.save({      ...user,      userAddresses: user.userAddresses,    })  }  console.timeEnd(`Create users intensive - TYPEORM`)}

Results and Conclusion

Create many plot

TypeORM and Prisma have performed almost the same in "Create Many" scenarios, with Prisma a little bit faster.

Create users in stress scenario plot

TypeORM had a far superior performance at creating new records in a stress scenario (many write requests per sec).

Find all users plot

TypeORM and Prisma have performed almost the same in "Find all" scenarios, with Prisma a little bit faster.

Find users by a given condition plot

TypeORM and Prisma have performed almost the same in "Find by a given condition" scenarios.

Query scale plot

Prisma started way faster in read queries, but as new records were being written to the database, Prisma gradually lost performance and then TypeORM became faster with more or less 1800 records in the database.

Links


Original Link: https://dev.to/josethz00/benchmark-prisma-vs-typeorm-3873

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To