Created
May 7, 2022 03:13
-
-
Save EtherDream/983c2d3beffb0cb90e72304b0fbaaaa6 to your computer and use it in GitHub Desktop.
instanceof is slow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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') | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
t0: 846.892822265625 ms
typeof: 862.230224609375 ms
bool: 957.297119140625 ms
instanceof: 2862.81787109375 ms