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.
Prisma has recently launched a new version that makes "create" and "update" operations faster 5.1.0
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 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.
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
TypeORM and Prisma have performed almost the same in "Create Many" scenarios, with Prisma a little bit faster.
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
TypeORM and Prisma have performed almost the same in "Find all" scenarios, with Prisma a little bit faster.
TypeORM and Prisma have performed almost the same in "Find by a given condition" scenarios.
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.
Top comments (7)
Interesting topic! 😁
I suspect in the Create Many that TypeORM uses async behind the scenes while Prisma doesn't which is the easiest explanation.
Could you please add Sequelize to this benchmarks? It's one of the most used ORMs in Node if not the most used one for relational DBs
Thank you! 😀
Yes, also, Prisma has its own modelling language, so certainly some time is spent in this process of translation from .ts to .prisma. In the next benchmark I will add Sequelize and MikroORM
i don't think so, as @joelbonetr said, probably TypeORM is doing some weird async thing to fetch the data. Prisma transpile all their .prisma -> .d.ts once you execute the
prisma generate
command. Another good strategy to see that more accurate is to create different users to the database, and then profile which queries are taking longer (you can get that by doing some queries on thepg_stat_activity
Perfect thank you! Following to see the results! :)
I think prisma query the new data after created. (in create users intensive case)
typeorm
prisma (line 3)
nice, thank you for the contribution
Great!