From b9c83859c50a45feb40842a5d2b09b948bf93a6f Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Wed, 16 Dec 2015 15:17:47 -0500 Subject: [PATCH] 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.) --- es6-shim.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/es6-shim.js b/es6-shim.js index bf40f23d..81fbaf4f 100644 --- a/es6-shim.js +++ b/es6-shim.js @@ -267,7 +267,9 @@ var $String = String; - var ES = { + // Assign these functions to a prototype to give v8 a hint that it + // should optimize them as constant functions (and inline them). + var ES = (function () {}).prototype = { // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args Call: function Call(F, V) { var args = arguments.length > 2 ? arguments[2] : []; @@ -2015,7 +2017,7 @@ // some environments don't have setTimeout - no way to shim here. if (typeof setTimeout !== 'function' && typeof setTimeout !== 'object') { return; } - ES.IsPromise = function (promise) { + var IsPromise = function (promise) { if (!ES.TypeIsObject(promise)) { return false; } @@ -2423,7 +2425,7 @@ if (!ES.TypeIsObject(C)) { throw new TypeError('Bad promise constructor'); } - if (ES.IsPromise(v)) { + if (IsPromise(v)) { var constructor = v.constructor; if (constructor === C) { return v; } } @@ -2441,7 +2443,7 @@ then: function then(onFulfilled, onRejected) { var promise = this; - if (!ES.IsPromise(promise)) { throw new TypeError('not a promise'); } + if (!IsPromise(promise)) { throw new TypeError('not a promise'); } var C = ES.SpeciesConstructor(promise, Promise); var resultCapability; var returnValueIsIgnored = (arguments.length > 2 && arguments[2] === PROMISE_FAKE_CAPABILITY);