Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Depending on the runtime implementation, Object in JavaScript should be somewhat similar to a HashMap. So a key lookup should be O(1). Array.includes on the other hand is obviously O(n). They really are not very comparable.
Object.hasOwnProperty would be more comparable to the in operator. in should be a little bit more complicated as it looks through the prototype chain as well. But for keys that are already attached to an Object, I suppose it should give a similar, if not the same, time complexity.
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
I bet it has to be
Object.hasOwnProperty
.Depending on the runtime implementation, Object in JavaScript should be somewhat similar to a HashMap. So a key lookup should be O(1).
Array.includes
on the other hand is obviously O(n). They really are not very comparable.Object.hasOwnProperty
would be more comparable to thein
operator.in
should be a little bit more complicated as it looks through the prototype chain as well. But for keys that are already attached to an Object, I suppose it should give a similar, if not the same, time complexity.Turns out the Array includes method is faster!
thepracticaldev.s3.amazonaws.com/i...
Try a bigger object, say with 20000 keys.
interesting, is this running on node?