Test name | Executions per second |
---|---|
instanceof | 38963.6 Ops/sec |
key in | 40305.4 Ops/sec |
const fooKey = Symbol('fooSymbol')
class Foo {
constructor(value) {
this[fooKey] = true
this.value = value
}
}
let arr;
function prepare() {
arr = Array.from({length:1000})
for (let i = 0; i < arr.length; i++) {
arr[i] = new Foo(i)
}
}
function runInstanceof() {
prepare();
const match = arr.every((foo) => foo instanceof Foo);
if (!match) throw new Error('fail!');
}
function runKeyIn() {
prepare();
const match = arr.every((foo) => fooKey in foo);
if (!match) throw new Error('fail!');
}
runInstanceof()
runKeyIn()