-
-
Save jasongrout/9daba69b2d84b78c5a8487cc0463de99 to your computer and use it in GitHub Desktop.
Undefined compare slow in Firefox
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
// Interestingly, the compare with undefined below is about ~10x slower than the typeof compare in Firefox. | |
// See this by running myTiming() and myTimingUndefinedCompare() | |
class MyArrayIterator { | |
constructor(source) { | |
this._index = 0; | |
this._source = source; | |
} | |
next() { | |
"use strict"; | |
if (this._index >= this._source.length) { | |
return undefined; | |
} | |
return this._source[this._index++]; | |
} | |
} | |
function mySum(it) { | |
"use strict"; | |
let v; | |
let r = 0; | |
while (typeof (v = it.next()) !== 'undefined') { | |
r += v; | |
} | |
return r; | |
} | |
function mySumUndefinedCompare(it) { | |
"use strict"; | |
let v; | |
let r = 0; | |
while ((v = it.next()) !== undefined) { | |
r += v; | |
} | |
return r; | |
} | |
function makeData() { | |
let data = []; | |
for (let i = 0; i < 1000000; ++i) { | |
data.push(Math.random()); | |
} | |
return data; | |
} | |
var data = makeData(); | |
function myTiming() { | |
"use strict"; | |
let it = new MyArrayIterator(data); | |
let t1 = performance.now(); | |
let r = mySum(it); | |
let t2 = performance.now(); | |
return [r, t2 - t1]; | |
} | |
function myTimingUndefinedCompare() { | |
"use strict"; | |
let it = new MyArrayIterator(data); | |
let t1 = performance.now(); | |
let r = mySumUndefinedCompare(it); | |
let t2 = performance.now(); | |
return [r, t2 - t1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment