Skip to content

Commit b9c8385

Browse files
committed
Ensure that ES.* abstract operations are able to be inlined.
Add a bogus assignment to a function prototype, which appears to be the magic hint to treat function assignments as constant (until proven otherwise). Also get rid of the after-the-fact mutation of the ES object by making IsPromise an ordinary function. (We could also call `Object.freeze(ES)` but it doesn't actually improve performance further.)
1 parent 859f415 commit b9c8385

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

es6-shim.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@
267267

268268
var $String = String;
269269

270-
var ES = {
270+
// Assign these functions to a prototype to give v8 a hint that it
271+
// should optimize them as constant functions (and inline them).
272+
var ES = (function () {}).prototype = {
271273
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args
272274
Call: function Call(F, V) {
273275
var args = arguments.length > 2 ? arguments[2] : [];
@@ -2015,7 +2017,7 @@
20152017
// some environments don't have setTimeout - no way to shim here.
20162018
if (typeof setTimeout !== 'function' && typeof setTimeout !== 'object') { return; }
20172019

2018-
ES.IsPromise = function (promise) {
2020+
var IsPromise = function (promise) {
20192021
if (!ES.TypeIsObject(promise)) {
20202022
return false;
20212023
}
@@ -2423,7 +2425,7 @@
24232425
if (!ES.TypeIsObject(C)) {
24242426
throw new TypeError('Bad promise constructor');
24252427
}
2426-
if (ES.IsPromise(v)) {
2428+
if (IsPromise(v)) {
24272429
var constructor = v.constructor;
24282430
if (constructor === C) { return v; }
24292431
}
@@ -2441,7 +2443,7 @@
24412443

24422444
then: function then(onFulfilled, onRejected) {
24432445
var promise = this;
2444-
if (!ES.IsPromise(promise)) { throw new TypeError('not a promise'); }
2446+
if (!IsPromise(promise)) { throw new TypeError('not a promise'); }
24452447
var C = ES.SpeciesConstructor(promise, Promise);
24462448
var resultCapability;
24472449
var returnValueIsIgnored = (arguments.length > 2 && arguments[2] === PROMISE_FAKE_CAPABILITY);

0 commit comments

Comments
 (0)