Skip to content

Commit

Permalink
perf: Use ES6 rest operator and allow V8 to optimize mergeOptions (#3743
Browse files Browse the repository at this point in the history
)

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`.
  • Loading branch information
misteroneill authored and gkatsev committed Nov 4, 2016
1 parent 6889e92 commit 5f42130
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/js/utils/merge-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

0 comments on commit 5f42130

Please sign in to comment.