Skip to content

Commit

Permalink
Merge branch 'xobotyi-code-optimisations'
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 2, 2019
2 parents 95aa50e + 865f8a5 commit f6ac6b6
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/index.jst
Expand Up @@ -2,13 +2,17 @@

// do not edit .js files directly - edit src/index.jst

{{? it.es6 }}
var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
{{?}}

module.exports = function equal(a, b) {
if (a === b) return true;

if (a && b && typeof a == 'object' && typeof b == 'object') {
if (a.constructor !== b.constructor) return false;

var length, i, key, keys;
var length, i, keys;
if (Array.isArray(a)) {
length = a.length;
if (length != b.length) return false;
Expand All @@ -20,25 +24,19 @@ module.exports = function equal(a, b) {
{{? it.es6 }}
if (a instanceof Map) {
if (a.size !== b.size) return false;
for(i of a.entries())
if (!b.has(i[0])) return false;

keys = getKeys(a);
for (i = a.size; i-- !== 0;)
if (!b.has(keys[i])) return false;

for (i = a.size; i-- !== 0;) {
key = keys[i];
if (!equal(a.get(key), b.get(key))) return false;
}
for(i of a.entries())
if (!equal(i[1], b.get(i[0]))) return false;

return true;
}

if (a instanceof Set) {
if (a.size !== b.size) return false;

keys = getKeys(a);
for (i = a.size; i-- !== 0;)
if (!b.has(keys[i])) return false;
for(i of a.entries())
if (!b.has(i[0])) return false;

return true;
}
Expand All @@ -53,8 +51,7 @@ module.exports = function equal(a, b) {
a instanceof Uint32Array ||
a instanceof Float32Array ||
a instanceof Float64Array ||
a instanceof BigInt64Array ||
a instanceof BigUint64Array
(envHasBigInt64Array && (a instanceof BigInt64Array || a instanceof BigUint64Array))
)) {
length = a.length;
if (length != b.length) return false;
Expand All @@ -76,7 +73,7 @@ module.exports = function equal(a, b) {
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;

for (i = length; i-- !== 0;) {
key = keys[i];
var key = keys[i];
if (!equal(a[key], b[key])) return false;
}

Expand All @@ -86,11 +83,3 @@ module.exports = function equal(a, b) {
// true if both NaN, false otherwise
return a!==a && b!==b;
};

{{? it.es6 }}
function getKeys(a) {
var keys = [];
for (var [key] of a.entries()) keys.push(key);
return keys;
}
{{?}}

0 comments on commit f6ac6b6

Please sign in to comment.