Benchmark: Prisma VS TypeORM
Tags: javascript, performance, database, programming
Published: 2022-08-16 19:01:00

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.

&nbsp;

## 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.

_Prisma has recently launched a new version that makes "create" and "update" operations faster [5.1.0](https://github.com/prisma/prisma/releases/tag/5.1.0)_

&nbsp;

## 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

- [TypeScript - v4.7.4](https://www.typescriptlang.org/)
- [@faker-js/faker - v7.4.0](https://www.npmjs.com/package/@faker-js/faker)
- [Node.js - v16.14.2](https://nodejs.org/en/)
- [Prisma - v4.2.1](https://www.prisma.io/)
- [TypeORM - v0.3.7](https://typeorm.io/)

### 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 Octa-core
  - 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.

&nbsp;

## 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**:

```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**:

```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**:

```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**:

```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**:

```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**:

```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**:

```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**:

```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`)
}
```

&nbsp;

## Results and Conclusion

![Create many plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcegayho7yiksmnoabc8.png)

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

![Create users in stress scenario plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qfvpaw4xz434xtl2t34b.png)

TypeORM had a far superior performance at creating new records in a stress scenario (many write requests per sec). _This might be outdated, due to recent Prisma updates [5.1.0](https://github.com/prisma/prisma/releases/tag/5.1.0)_

![Find all users plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jubrkygpka4bnf274ff.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ex39yyyshlz4v2r5k47s.png)

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

![Query scale plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgrzsj4tydmdbql1fa23.png)

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

- [Other benchmark](https://edgedb.github.io/imdbench/js.html)
- [Code that I used to produce this benchmark](https://github.com/josethz00/prisma-vs-typeorm)