Skip to content

Commit

Permalink
Add more Map/Set fast paths for more primitives: boolean, null, u…
Browse files Browse the repository at this point in the history
…ndefined.
  • Loading branch information
ljharb committed May 29, 2015
1 parent f904808 commit db7a093
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
26 changes: 19 additions & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1945,14 +1945,18 @@
return null;
}
var type = typeof key;
if (type === 'string') {
if (type === 'undefined' || key === null) {
return '^' + String(key);
} else if (type === 'string') {
return '$' + key;
} else if (type === 'number') {
// note that -0 will get coerced to "0" when used as a property key
if (!preservesNumericInsertionOrder) {
return 'n' + key;
}
return key;
} else if (type === 'boolean') {
return 'b' + key;
}
return null;
};
Expand Down Expand Up @@ -2280,13 +2284,21 @@
if (!set['[[SetData]]']) {
var m = set['[[SetData]]'] = new collectionShims.Map();
_forEach(Object.keys(set._storage), function (k) {
// fast check for leading '$'
if (k.charCodeAt(0) === 36) {
k = k.slice(1);
} else if (k.charAt(0) === 'n') {
k = +k.slice(1);
if (k === '^null') {
k = null;
} else if (k === '^undefined') {
k = void 0;
} else {
k = +k;
var first = k.charAt(0);
if (first === '$') {
k = k.slice(1);
} else if (first === 'n') {
k = +k.slice(1);
} else if (first === 'b') {
k = k === 'btrue';
} else {
k = +k;
}
}
m.set(k, k);
});
Expand Down
14 changes: 11 additions & 3 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,15 @@ describe('Collections', function () {

var set;
beforeEach(function () {
set = new Set([1, NaN, false]);
set = new Set([1, NaN, false, true, null, undefined, 'a']);
});

afterEach(function () {
set = null;
});

it('works with the full set', function () {
expect(Array.from(set)).to.eql([1, NaN, false]);
expect(Array.from(set)).to.eql([1, NaN, false, true, null, undefined, 'a']);
});

it('works with Set#keys()', function () {
Expand All @@ -837,7 +837,15 @@ describe('Collections', function () {
});

it('works with Set#entries()', function () {
expect(Array.from(set.entries())).to.eql([[1, 1], [NaN, NaN], [false, false]]);
expect(Array.from(set.entries())).to.eql([
[1, 1],
[NaN, NaN],
[false, false],
[true, true],
[null, null],
[undefined, undefined],
['a', 'a']
]);
});
});

Expand Down

0 comments on commit db7a093

Please sign in to comment.