Is the difference between them is just syntax or there's a performance issue?
Both, and more...
Rest parameters:
- Are a known idiom in other languages (Ruby, Python).
- Are easier to read and maintain (vs.
slice). - Are easier to understand for beginners.
- Can (and likely will) result in better performance, since engines can optimize.
- Are tool friendlier, as they can be analyzed statically.
Is the difference between them is just syntax or there's a performance issue?
Both, and more...
Rest parameters:
- Are a known idiom in other languages (Ruby, Python).
- Are easier to read and maintain (vs.
slice). - Are easier to understand for beginners.
- Can (and likely will) result in better performance, since engines can optimize.
- Are tool friendlier, as they can be analyzed statically.
In addition to @kangax’s response, I would elaborate that performance and correctness are problematic many times the arguments object is invoked. If you pass the arguments object to another function, you pass with it the right to modify the corresponding local variables in your scope.
function foo(arg) {
bar(arguments);
return arg;
}
function bar(args) {
args[0] = 20;
}
foo(10) // 20
The existence of this feature invalidates local reasoning within a function, which makes JIT optimization more difficult and introduces certain security hazards. Programs that are designed for speed must work around this problem with far more elaborate boilerplate code than the Array.prototype.slice.call(arguments) idiom, and in security contexts, the passing of arguments must be strictly disallowed.
Eliminating the need to use the arguments object to extract variadic arguments is one of the many advantages to the new syntax.
spread vs rest syntax in JS, a little confused.
How to Use the Spread Operator (...) In JavaScript
Thank you for sharing this. You made my day and helped me learn something really useful.
Have a nice day :)
More on reddit.comWhat is the difference between the rest and spread operator?
Rest and Spread operator: Three dots that changed JavaScript
I wouldn't recommend naming the function parameter of a reduce previous.
function sum(...args) {
return args.reduce((previous, current)=> {
return previous + current;
});
}
The first parameter is the accumulator. total or previousTotal or anything that suggests that we're building on it may be better.
previous suggests that we're comparing to the previous item in the array, and examples that use it can make reduce functions even more complicated to understand than they need to be.