4

Provided that you're looking for something based on its primary key and don't want to include any additional options, does it matter whether you use findOne or findByPk? Would the performance be the same?

2 Answers 2

7

Model.findByPk() use Model.findOne() internally, you can see it from v6.19.0/src/model.js#L1933. Both of these methods are a shortcut.

In the end, both methods call the more general Model.findAll() method, look v6.19.0/src/model.js#L1968.

Both of them just do some parameter verification and create where clause options that have a negligible impact on performance.

The only difference is the usage.

Model.findByPk(1) is equivalent to Model.findOne({primaryKey: 1}), you need to specify the primary key by yourself.

1

Under the hood, there is no difference between passing a pk to findOne or using findByPk, i.e the performance is the same.

It's just an abstraction that sequelize gives in order to get a much readable code.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.