From 1d83ad6cceb82d104f440e15cdbe5e15dd065b1f Mon Sep 17 00:00:00 2001 From: kpdecker Date: Thu, 25 Jun 2015 09:22:03 -0500 Subject: [PATCH] Avoid defineProperty when not needed This lets us use the fast path for most object literal assignments and then utilizes the defineProperty path when there is a chance that we could hit the setter issue described in #357. 10x performance boosts seen for the six-speed test case, going from 200k operations/sec to 2M ops/sec. --- .../templates/helper-define-property.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/babel/transformation/templates/helper-define-property.js b/src/babel/transformation/templates/helper-define-property.js index dca42eeabb..2560c05c45 100644 --- a/src/babel/transformation/templates/helper-define-property.js +++ b/src/babel/transformation/templates/helper-define-property.js @@ -1,8 +1,18 @@ (function (obj, key, value) { - return Object.defineProperty(obj, key, { - value: value, - enumerable: true, - configurable: true, - writable: true - }); + // Shortcircuit the slow defineProperty path when possible. + // We are trying to avoid issues where setters defined on the + // prototype cause side effects under the fast path of simple + // assignment. By checking for existence of the property with + // the in operator, we can optimize most of this overhead away. + if (key in obj) { + Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } else { + obj[key] = value; + } + return obj; });