From 32f8f9e663919312a016211d3a0c5edfa8f13079 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 23 Nov 2014 21:43:28 +1100 Subject: [PATCH] change arguments to array to an additional faster helper method --- .../templates/arguments-slice-assign-arg.js | 1 - lib/6to5/templates/arguments-slice-assign.js | 1 - lib/6to5/templates/arguments-slice.js | 1 - lib/6to5/templates/arguments-to-array.js | 7 +++++ lib/6to5/templates/object-spread.js | 9 +++++++ lib/6to5/templates/slice.js | 1 - .../transformers/es6-rest-parameters.js | 19 +++++++++----- .../accessing-super-class/expected.js | 26 ++++++++++++------- .../arrow-functions/expected.js | 15 ++++++++--- .../es6-rest-parameters/multiple/expected.js | 14 +++++++--- .../es6-rest-parameters/single/expected.js | 14 +++++++--- .../es6-spread/arguments/expected.js | 12 +++++++-- .../misc/custom-runtime/expected.js | 2 +- .../transformation/misc/runtime/expected.js | 2 +- 14 files changed, 91 insertions(+), 33 deletions(-) delete mode 100644 lib/6to5/templates/arguments-slice-assign-arg.js delete mode 100644 lib/6to5/templates/arguments-slice-assign.js delete mode 100644 lib/6to5/templates/arguments-slice.js create mode 100644 lib/6to5/templates/arguments-to-array.js create mode 100644 lib/6to5/templates/object-spread.js delete mode 100644 lib/6to5/templates/slice.js diff --git a/lib/6to5/templates/arguments-slice-assign-arg.js b/lib/6to5/templates/arguments-slice-assign-arg.js deleted file mode 100644 index 544c56192d..0000000000 --- a/lib/6to5/templates/arguments-slice-assign-arg.js +++ /dev/null @@ -1 +0,0 @@ -var VARIABLE_NAME = SLICE_KEY.call(arguments, SLICE_ARG); diff --git a/lib/6to5/templates/arguments-slice-assign.js b/lib/6to5/templates/arguments-slice-assign.js deleted file mode 100644 index 90d412107e..0000000000 --- a/lib/6to5/templates/arguments-slice-assign.js +++ /dev/null @@ -1 +0,0 @@ -var VARIABLE_NAME = SLICE_KEY.call(arguments); diff --git a/lib/6to5/templates/arguments-slice.js b/lib/6to5/templates/arguments-slice.js deleted file mode 100644 index 139d78556f..0000000000 --- a/lib/6to5/templates/arguments-slice.js +++ /dev/null @@ -1 +0,0 @@ -SLICE_KEY.call(arguments); diff --git a/lib/6to5/templates/arguments-to-array.js b/lib/6to5/templates/arguments-to-array.js new file mode 100644 index 0000000000..129e2b89e5 --- /dev/null +++ b/lib/6to5/templates/arguments-to-array.js @@ -0,0 +1,7 @@ +(function (args) { + var target = new Array(args.length); + for (var i = 0; i< args.length; i++) { + target[i] = args[i]; + } + return target; +}) diff --git a/lib/6to5/templates/object-spread.js b/lib/6to5/templates/object-spread.js new file mode 100644 index 0000000000..901fb418c6 --- /dev/null +++ b/lib/6to5/templates/object-spread.js @@ -0,0 +1,9 @@ +(function (target, keys) { + var target = {}; + for (var i in target) { + if (keys.indexOf(i) >= 0) continue; + if (!Object.prototype.hasOwn.call(target)) continue; + target[i] = target[i]; + } + return target; +}) diff --git a/lib/6to5/templates/slice.js b/lib/6to5/templates/slice.js deleted file mode 100644 index f35f3b503b..0000000000 --- a/lib/6to5/templates/slice.js +++ /dev/null @@ -1 +0,0 @@ -Array.prototype.slice; diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index df49a848c5..08bddd95da 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -12,13 +12,18 @@ exports.Function = function (node, parent, file) { t.ensureBlock(node); - var template = util.template(templateName, { - SLICE_KEY: file.addDeclaration("slice"), - VARIABLE_NAME: rest, - SLICE_ARG: t.literal(node.params.length) - }); + var call = t.callExpression( + file.addDeclaration("arguments-to-array"), + [t.identifier("arguments")] + ); - template.declarations[0].init.arguments[0]._ignoreAliasFunctions = true; + if (node.params.length) { + call = t.callExpression(t.memberExpression(call, t.identifier("slice")), [t.literal(node.params.length)]); + } - node.body.body.unshift(template); + call._ignoreAliasFunctions = true; + + node.body.body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(rest, call) + ])); }; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index d49fffb74e..59bdbf71ad 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -1,6 +1,14 @@ "use strict"; -var _slice = Array.prototype.slice; +var _argumentsToArray = function (args) { + var target = new Array(args.length); + for (var i = 0; i < args.length; i++) { + target[i] = args[i]; + } + + return target; +}; + var _classProps = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); @@ -25,11 +33,11 @@ var Test = (function (Foo) { Foo.prototype.test.call(this); foob(Foo); - Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); + Foo.call.apply(Foo, [this].concat(_argumentsToArray(arguments))); + Foo.call.apply(Foo, [this, "test"].concat(_argumentsToArray(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_argumentsToArray(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_argumentsToArray(arguments))); }; _extends(Test, Foo); @@ -39,8 +47,8 @@ var Test = (function (Foo) { writable: true, value: function () { Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this].concat(_argumentsToArray(arguments))); + Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_argumentsToArray(arguments))); } } }, { @@ -48,8 +56,8 @@ var Test = (function (Foo) { writable: true, value: function () { Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_argumentsToArray(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_argumentsToArray(arguments))); } } }); diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index 49c1d11e53..744eae39ec 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,6 +1,15 @@ "use strict"; -var _slice = Array.prototype.slice; -var concat = function () { - var arrs = _slice.call(arguments); +var _arguments = arguments; +var _argumentsToArray = function (args) { + var target = new Array(args.length); + for (var i = 0; i < args.length; i++) { + target[i] = args[i]; + } + + return target; +}; + +var concat = function () { + var arrs = _argumentsToArray(_arguments); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index f2f9ea4fe4..c5a26f5041 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,10 +1,18 @@ "use strict"; -var _slice = Array.prototype.slice; +var _argumentsToArray = function (args) { + var target = new Array(args.length); + for (var i = 0; i < args.length; i++) { + target[i] = args[i]; + } + + return target; +}; + var t = function (f) { - var items = _slice.call(arguments, 1); + var items = _argumentsToArray(arguments).slice(1); }; function t(f) { - var items = _slice.call(arguments, 1); + var items = _argumentsToArray(arguments).slice(1); } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index 306e5f6f68..3746578ccd 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,10 +1,18 @@ "use strict"; -var _slice = Array.prototype.slice; +var _argumentsToArray = function (args) { + var target = new Array(args.length); + for (var i = 0; i < args.length; i++) { + target[i] = args[i]; + } + + return target; +}; + var t = function () { - var items = _slice.call(arguments); + var items = _argumentsToArray(arguments); }; function t() { - var items = _slice.call(arguments); + var items = _argumentsToArray(arguments); } diff --git a/test/fixtures/transformation/es6-spread/arguments/expected.js b/test/fixtures/transformation/es6-spread/arguments/expected.js index 1ba8c3aeb2..11cafe747c 100644 --- a/test/fixtures/transformation/es6-spread/arguments/expected.js +++ b/test/fixtures/transformation/es6-spread/arguments/expected.js @@ -1,8 +1,16 @@ "use strict"; -var _slice = Array.prototype.slice; +var _argumentsToArray = function (args) { + var target = new Array(args.length); + for (var i = 0; i < args.length; i++) { + target[i] = args[i]; + } + + return target; +}; + function foo() { - return bar.apply(null, ["test"].concat(_slice.call(arguments))); + return bar.apply(null, ["test"].concat(_argumentsToArray(arguments))); } function bar(one, two, three) { diff --git a/test/fixtures/transformation/misc/custom-runtime/expected.js b/test/fixtures/transformation/misc/custom-runtime/expected.js index 94200c84ce..41092c40d2 100644 --- a/test/fixtures/transformation/misc/custom-runtime/expected.js +++ b/test/fixtures/transformation/misc/custom-runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = customNamespace.slice.call(arguments); + var test = customNamespace.argumentsToArray(arguments); } diff --git a/test/fixtures/transformation/misc/runtime/expected.js b/test/fixtures/transformation/misc/runtime/expected.js index 1427ac1536..e7364114e7 100644 --- a/test/fixtures/transformation/misc/runtime/expected.js +++ b/test/fixtures/transformation/misc/runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = to5Runtime.slice.call(arguments); + var test = to5Runtime.argumentsToArray(arguments); }