Skip to content

Commit

Permalink
Optimize Map/Set fast key path.
Browse files Browse the repository at this point in the history
The expression `typeof x === 'some string'` is treated as a peephole
optimization by JavaScript runtimes and optimized as a simple type test
without an explicit string comparison.  Refactoring the `typeof x` part
into a variable cause this to be deoptimized: the type information
is lost and we end up doing actual string comparison operations at
runtime.
  • Loading branch information
cscott committed Jan 10, 2016
1 parent 2ab34d8 commit 1d0888d
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions es6-shim.js
Expand Up @@ -2631,18 +2631,17 @@
if (!preservesInsertionOrder) {
return null;
}
var type = typeof key;
if (type === 'undefined' || key === null) {
if (typeof key === 'undefined' || key === null) {
return '^' + ES.ToString(key);
} else if (type === 'string') {
} else if (typeof key === 'string') {
return '$' + key;
} else if (type === 'number') {
} else if (typeof key === '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') {
} else if (typeof key === 'boolean') {
return 'b' + key;
}
return null;
Expand Down

0 comments on commit 1d0888d

Please sign in to comment.