Skip to content

Instantly share code, notes, and snippets.

@jasongrout
Forked from sccolbert/esIterTest.js
Last active April 26, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasongrout/9daba69b2d84b78c5a8487cc0463de99 to your computer and use it in GitHub Desktop.
Save jasongrout/9daba69b2d84b78c5a8487cc0463de99 to your computer and use it in GitHub Desktop.
Undefined compare slow in Firefox
// 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