From 5f42130b82fceadd49451f9c7ed95a53e3395eec Mon Sep 17 00:00:00 2001 From: Pat O'Neill Date: Fri, 4 Nov 2016 14:19:50 -0400 Subject: [PATCH] perf: Use ES6 rest operator and allow V8 to optimize mergeOptions (#3743) The current implementation causes the `mergeOptions` function to be de-optimized. This change improves readability by switching to ES6 syntax AND it results in a roughly 75% improvement in the performance of this function by transpiling to a `for` loop instead of `slice.call`. --- src/js/utils/merge-options.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/js/utils/merge-options.js b/src/js/utils/merge-options.js index 91dea8fb9a..2a1849d647 100644 --- a/src/js/utils/merge-options.js +++ b/src/js/utils/merge-options.js @@ -41,20 +41,17 @@ function customizer(destination, source) { * provided objects * @function mergeOptions */ -export default function mergeOptions() { - // contruct the call dynamically to handle the variable number of - // objects to merge - const args = Array.prototype.slice.call(arguments); +export default function mergeOptions(...objects) { // unshift an empty object into the front of the call as the target // of the merge - args.unshift({}); + objects.unshift({}); // customize conflict resolution to match our historical merge behavior - args.push(customizer); + objects.push(customizer); - merge.apply(null, args); + merge.apply(null, objects); // return the mutated result object - return args[0]; + return objects[0]; }