Skip to content

Commit 323f3cf

Browse files
shvaikaleshmeeber
authored andcommitted
perf: improve FakeMap implementation (#41)
1 parent 1fa5334 commit 323f3cf

5 files changed

Lines changed: 25 additions & 40 deletions

File tree

bench/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"es6": true
77
},
88
"rules": {
9+
"no-eval": 0,
910
"no-new-wrappers": 0,
1011
"no-array-constructor": 0,
1112
"no-new-object": 0,

bench/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ var fixtures = {
5757
};
5858
try {
5959
fixtures['arrow function (differing) '] = [
60-
eval('() => {}'), // eslint-disable-line no-eval
61-
eval('() => {}'), // eslint-disable-line no-eval
60+
eval('() => {}'),
61+
eval('() => {}'),
6262
false,
6363
];
6464
} catch (error) {
6565
console.error('cannot benchmark arrow functions');
6666
}
6767
try {
6868
fixtures['generator func (differing) '] = [
69-
eval('function * generator() {}; generator'), // eslint-disable-line no-eval
70-
eval('function * generator() {}; generator'), // eslint-disable-line no-eval
69+
eval('(function* () {})'),
70+
eval('(function* () {})'),
7171
false,
7272
];
7373
} catch (error) {
@@ -78,7 +78,7 @@ function prepareBenchMark(test, name, assert) {
7878
assert = assert || deepEql;
7979
var leftHand = test[0];
8080
var rightHand = test[1];
81-
var expectedResult = Boolean(2 in test ? test[2] : true);
81+
var expectedResult = 2 in test ? test[2] : true;
8282
var invocationString = 'deepEql(' + inspect(leftHand) + ', ' + inspect(rightHand) + ') === ' + expectedResult;
8383
benches.push(new Benchmark(name, {
8484
fn: function () {

index.js

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,31 @@
11
'use strict';
2-
/* globals Symbol: true, Uint8Array: true, WeakMap: true */
2+
/* globals Symbol: false, Uint8Array: false, WeakMap: false */
33
/*!
44
* deep-eql
55
* Copyright(c) 2013 Jake Luer <jake@alogicalparadox.com>
66
* MIT Licensed
77
*/
88

9-
/*!
10-
* Module dependencies
11-
*/
12-
139
var type = require('type-detect');
1410
function FakeMap() {
15-
this.clear();
11+
this._key = 'chai/deep-eql__' + Math.random() + Date.now();
1612
}
13+
1714
FakeMap.prototype = {
18-
clear: function clearMap() {
19-
this.keys = [];
20-
this.values = [];
21-
return this;
22-
},
23-
set: function setMap(key, value) {
24-
var index = this.keys.indexOf(key);
25-
if (index >= 0) {
26-
this.values[index] = value;
27-
} else {
28-
this.keys.push(key);
29-
this.values.push(value);
30-
}
31-
return this;
32-
},
3315
get: function getMap(key) {
34-
return this.values[this.keys.indexOf(key)];
16+
return key[this._key];
3517
},
36-
delete: function deleteMap(key) {
37-
var index = this.keys.indexOf(key);
38-
if (index >= 0) {
39-
this.values = this.values.slice(0, index).concat(this.values.slice(index + 1));
40-
this.keys = this.keys.slice(0, index).concat(this.keys.slice(index + 1));
18+
set: function setMap(key, value) {
19+
if (!Object.isFrozen(key)) {
20+
Object.defineProperty(key, this._key, {
21+
value: value,
22+
configurable: true,
23+
});
4124
}
42-
return this;
4325
},
4426
};
4527

46-
var MemoizeMap = null;
47-
if (typeof WeakMap === 'function') {
48-
MemoizeMap = WeakMap;
49-
} else {
50-
MemoizeMap = FakeMap;
51-
}
52-
28+
var MemoizeMap = typeof WeakMap === 'function' ? WeakMap : FakeMap;
5329
/*!
5430
* Check to see if the MemoizeMap has recorded a result of the two operands
5531
*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"rules": {
4949
"complexity": 0,
5050
"spaced-comment": 0,
51+
"no-underscore-dangle": 0,
5152
"no-use-before-define": 0
5253
}
5354
},

test/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ describe('Generic', function () {
332332
'eql({ foo: 1, bar: -> }, { foo: 1, bar: <- }) === true');
333333
});
334334

335+
it('returns true with frozen objects', function () {
336+
var objectA = Object.freeze({ foo: 1 });
337+
var objectB = Object.freeze({ foo: 1 });
338+
assert(eql(objectA, objectB) === true,
339+
'eql(Object.freeze({ foo: 1 }), Object.freeze({ foo: 1 })) === true');
340+
});
341+
335342
it('returns false with objects with deeply unequal prototypes', function () {
336343
var objectA = Object.create({ foo: { a: 1 } });
337344
var objectB = Object.create({ foo: { a: 2 } });

0 commit comments

Comments
 (0)