You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know it might be annoying to have two sets of "same code", but modern JavaScript engines optimize ES6 code which means it is possible to reduce uglified JS size by about 400 bytes without sacrificing any performance (except on legacy JS engines).
So here is a version based on ES6 from beta, but with optimizations that reduce code size. On my machine this seems to give more ops/sec than the current beta ES6 version, although the difference is pretty small. Also I guess the benchmark does not yet include anything to test perf of ES6 features.
Optimizations
BigInt arrays have one less boolean check on each iteration.
constructor is stored in local variable and all calls using it are immediately after.
Arrays and Typed arrays use the same path (with different comparison).
Maps and Sets use the same path (Map has one extra comparison).
Using .every() extensively to reduce code size.
Optimizations that use combined path will of course have slightly worse performance than if they were written separate (due to extra boolean storage & check per path). However the reduction in ops/sec is so tiny that I'd prefer shorter code due to slightly faster download and initial parsing on browser.
consttyped=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];if(typeofBigInt64Array!=='undefined')typed.push(BigInt64Array,BigUint64Array);module.exports=functionequal(a,b){if(a===b)returntrue;if(!a||!b||typeofa!=='object'||typeofb!=='object')// true if both NaN, false otherwisereturna!==a&&b!==b;constconstructor=a.constructor;if(constructor!==b.constructor)returnfalse;if(constructor===RegExp)returna.source===b.source&&a.flags===b.flags;constisArray=Array.isArray(a);// .some(...) -> .includes(constructor) would be shorted, but not sure if that works correctlyif(isArray||(constructor.BYTES_PER_ELEMENT&&typed.some(type=>ainstanceoftype))){leti=a.length;if(i===b.length){if(isArray)while(i-->0&&equal(a[i],b[i]));elsewhile(i-->0&&a[i]===b[i]);}returni===-1;}constisMap=ainstanceofMap;if(isMap||ainstanceofSet){if(a.size!==b.size)returnfalse;if(!a.keys().every(b.has))returnfalse;if(isMap&&!a.keys().every(key=>equal(a.get(key),b.get(key))))returnfalse;returntrue;}if(a.valueOf!==Object.prototype.valueOf)returna.valueOf()===b.valueOf();if(a.toString!==Object.prototype.toString)returna.toString()===b.toString();constkeys=Object.keys(a);if(keys.length!==Object.keys(b).length)returnfalse;if(!keys.every(Object.prototype.hasOwnProperty.bind(b)))returnfalse;returnkeys.every(key=>equal(a[key],b[key]));}
Uglified size: 1075 bytes.
Current ES6 beta uglified: 1473 bytes.
Edit: Although gzipped size difference is about ten bytes, so actual download size is about the same thus making the point of smaller download size near invalid :)
The text was updated successfully, but these errors were encountered:
I know it might be annoying to have two sets of "same code", but modern JavaScript engines optimize ES6 code which means it is possible to reduce uglified JS size by about 400 bytes without sacrificing any performance (except on legacy JS engines).
So here is a version based on ES6 from beta, but with optimizations that reduce code size. On my machine this seems to give more ops/sec than the current beta ES6 version, although the difference is pretty small. Also I guess the benchmark does not yet include anything to test perf of ES6 features.
Optimizations
constructor
is stored in local variable and all calls using it are immediately after..every()
extensively to reduce code size.Optimizations that use combined path will of course have slightly worse performance than if they were written separate (due to extra boolean storage & check per path). However the reduction in ops/sec is so tiny that I'd prefer shorter code due to slightly faster download and initial parsing on browser.
Uglified size: 1075 bytes.
Current ES6 beta uglified: 1473 bytes.
Edit: Although gzipped size difference is about ten bytes, so actual download size is about the same thus making the point of smaller download size near invalid :)
The text was updated successfully, but these errors were encountered: