Skip to content

Commit

Permalink
Some JS engines don't preserve insertion order for numeric keys.
Browse files Browse the repository at this point in the history
Fixes #290.
  • Loading branch information
cscott authored and ljharb committed Oct 2, 2014
1 parent 8b8c842 commit c16e859
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1511,14 +1511,30 @@
}

// Map and Set require a true ES5 environment
// Their fast path also requires that the environment preserve
// property insertion order, which is not guaranteed by the spec.
var testOrder = function(a) {
var b = Object.keys(a.reduce(function(o, k) { o[k] = true; return o; }, {}));
return a.join(':') === b.join(':');
};
var preservesInsertionOrder = testOrder(['z','a','bb']);
// some engines (eg, Chrome) only preserve insertion order for string keys
var preservesNumericInsertionOrder = testOrder(['z',1,'a','3',2]);

if (supportsDescriptors) {

var fastkey = function fastkey(key) {
if (!preservesInsertionOrder) {
return null;
}
var type = typeof key;
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;
}
return null;
Expand Down Expand Up @@ -1813,6 +1829,8 @@
// fast check for leading '$'
if (k.charCodeAt(0) === 36) {
k = k.slice(1);
} else if (k.charCodeAt(0) === 110 /* 'n' */) {
k = +(k.slice(1));
} else {
k = +k;
}
Expand Down

0 comments on commit c16e859

Please sign in to comment.