Is findOne(id) faster than findOne({ id }) in TypeORM with PostgreSQL?

Viewed 2960

I'm using NestJS framework with TypeORM and PostgreSQL. Let's say I have a simple User entity which has only two properties: id and name

@Entity()
class User {
  @PrimaryGeneratedColumn()
  id: string;

  @Column({
    type: 'varchar'
  })
  name: string;

Questions:

  1. Is usersRepository.findOne(id) faster than usersRepository.findOne({ id })?

  2. I assume that usersRepository.findOne(id) will be faster than usersRepository.findOne({ name }) since name is not in index, won't it?

1 Answers

I created a table with 500 000 items and benchmarked some methods (the methods have been called 100 000 times):

Method Avg time [µs]
this.usersRepository.query('SELECT * FROM "users" WHERE id = 258') 163.98
this.usersRepository.query('SELECT * FROM "users" WHERE name = "John"') 143.77
this.usersRepository.findByIds([258]) 343.99
this.usersRepository.findOne(258) 326.64
this.usersRepository.findOne({ id: 258 }) 338.89
this.usersRepository.findOne({ name: "John" }) 298.40

Then it looks like findOne(id) isn't (much) faster than findOne({ id }). Moreover, findOne({ name }) has similar results. However, it looks like using custom queries is the best option.

Related