Skip to content

Commit

Permalink
Ensure that ES.* abstract operations are able to be inlined.
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
cscott committed Dec 16, 2015
1 parent 859f415 commit b9c8385
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] : [];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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; }
}
Expand All @@ -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);
Expand Down

0 comments on commit b9c8385

Please sign in to comment.