Skip to content

Instantly share code, notes, and snippets.

@EtherDream
Created May 7, 2022 03:13
Show Gist options
  • Save EtherDream/983c2d3beffb0cb90e72304b0fbaaaa6 to your computer and use it in GitHub Desktop.
Save EtherDream/983c2d3beffb0cb90e72304b0fbaaaa6 to your computer and use it in GitHub Desktop.
instanceof is slow
(function() {
'use strict'
let s = fetch('/')
console.time('t0')
for (let i = 0; i < 1e9; i++) {
if (true) {}
}
console.timeEnd('t0')
console.time('typeof')
for (let i = 0; i < 1e9; i++) {
if (typeof s.then === 'function') {}
}
console.timeEnd('typeof')
console.time('bool')
for (let i = 0; i < 1e9; i++) {
if (!!s.then) {
}
}
console.timeEnd('bool')
console.time('instanceof')
for (let i = 0; i < 1e9; i++) {
if (s instanceof Promise) {}
}
console.timeEnd('instanceof')
})()
@EtherDream
Copy link
Author

t0: 846.892822265625 ms
typeof: 862.230224609375 ms
bool: 957.297119140625 ms
instanceof: 2862.81787109375 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment