Skip to content

Commit

Permalink
perf: improve FakeMap implementation (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
shvaikalesh authored and meeber committed Aug 5, 2017
1 parent 1fa5334 commit 323f3cf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 40 deletions.
1 change: 1 addition & 0 deletions bench/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"es6": true
},
"rules": {
"no-eval": 0,
"no-new-wrappers": 0,
"no-array-constructor": 0,
"no-new-object": 0,
Expand Down
10 changes: 5 additions & 5 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ var fixtures = {
};
try {
fixtures['arrow function (differing) '] = [
eval('() => {}'), // eslint-disable-line no-eval
eval('() => {}'), // eslint-disable-line no-eval
eval('() => {}'),
eval('() => {}'),
false,
];
} catch (error) {
console.error('cannot benchmark arrow functions');
}
try {
fixtures['generator func (differing) '] = [
eval('function * generator() {}; generator'), // eslint-disable-line no-eval
eval('function * generator() {}; generator'), // eslint-disable-line no-eval
eval('(function* () {})'),
eval('(function* () {})'),
false,
];
} catch (error) {
Expand All @@ -78,7 +78,7 @@ function prepareBenchMark(test, name, assert) {
assert = assert || deepEql;
var leftHand = test[0];
var rightHand = test[1];
var expectedResult = Boolean(2 in test ? test[2] : true);
var expectedResult = 2 in test ? test[2] : true;
var invocationString = 'deepEql(' + inspect(leftHand) + ', ' + inspect(rightHand) + ') === ' + expectedResult;
benches.push(new Benchmark(name, {
fn: function () {
Expand Down
46 changes: 11 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
'use strict';
/* globals Symbol: true, Uint8Array: true, WeakMap: true */
/* globals Symbol: false, Uint8Array: false, WeakMap: false */
/*!
* deep-eql
* Copyright(c) 2013 Jake Luer <[email protected]>
* MIT Licensed
*/

/*!
* Module dependencies
*/

var type = require('type-detect');
function FakeMap() {
this.clear();
this._key = 'chai/deep-eql__' + Math.random() + Date.now();
}

FakeMap.prototype = {
clear: function clearMap() {
this.keys = [];
this.values = [];
return this;
},
set: function setMap(key, value) {
var index = this.keys.indexOf(key);
if (index >= 0) {
this.values[index] = value;
} else {
this.keys.push(key);
this.values.push(value);
}
return this;
},
get: function getMap(key) {
return this.values[this.keys.indexOf(key)];
return key[this._key];
},
delete: function deleteMap(key) {
var index = this.keys.indexOf(key);
if (index >= 0) {
this.values = this.values.slice(0, index).concat(this.values.slice(index + 1));
this.keys = this.keys.slice(0, index).concat(this.keys.slice(index + 1));
set: function setMap(key, value) {
if (!Object.isFrozen(key)) {
Object.defineProperty(key, this._key, {
value: value,
configurable: true,
});
}
return this;
},
};

var MemoizeMap = null;
if (typeof WeakMap === 'function') {
MemoizeMap = WeakMap;
} else {
MemoizeMap = FakeMap;
}

var MemoizeMap = typeof WeakMap === 'function' ? WeakMap : FakeMap;
/*!
* Check to see if the MemoizeMap has recorded a result of the two operands
*
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"rules": {
"complexity": 0,
"spaced-comment": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0
}
},
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ describe('Generic', function () {
'eql({ foo: 1, bar: -> }, { foo: 1, bar: <- }) === true');
});

it('returns true with frozen objects', function () {
var objectA = Object.freeze({ foo: 1 });
var objectB = Object.freeze({ foo: 1 });
assert(eql(objectA, objectB) === true,
'eql(Object.freeze({ foo: 1 }), Object.freeze({ foo: 1 })) === true');
});

it('returns false with objects with deeply unequal prototypes', function () {
var objectA = Object.create({ foo: { a: 1 } });
var objectB = Object.create({ foo: { a: 2 } });
Expand Down

0 comments on commit 323f3cf

Please sign in to comment.