ben-ng / add

A cross-browser, numerically stable algorithm that adds floats accurately in Javascript
https://www.npmjs.org/package/add
45 stars 13 forks source link

Allow values to be specified as parameters #5

Open hiddentao opened 10 years ago

hiddentao commented 10 years ago

Even though #4 wasn't a massive performance improvement, being able to pass in parameters is more efficient when you have say 2 or 3 values you wish to add and they're not stored in an array. Because it saves having to construct an array.

And inside the add() function we can simply get everything as an array using arguments. If it doesn't impact performance I propose switching the function signature to use parameters. And also note that the upcoming ES6 function splats will make it easier to get a real Array from multiple function parameters.

joeybaker commented 10 years ago

I'd support the API change. We should just be very careful about an arguments implementation: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments

ben-ng commented 10 years ago

I have not tested this because the algorithms that use add aren't written yet (dot product to summation transform) but i have read from other people's experiences that arguments is slower.

If you want to benchmark this change, keep in mind that in practice add will be used on massive arrays, even though the benchmarks are for very small arrays right now.

hiddentao commented 10 years ago

We could add another function - addParam() like so:

var addParam = () {
  add.apply(null, arguments);
}

This would be ok to do from an optimization perspective - https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#what-is-safe-arguments-usage

Although actually checking with the V8 optimizer would be worth it too just to be sure.

ben-ng commented 10 years ago

Since it's not possible to get a true benchmark of the difference between .apply and passing an array, I think we should hold off on this change until I'm done with the dot product/matrix multiplication algorithm.

Add was not intended to be used on small numbers of arguments because the error there is insignificant anyway. It's meant to be used on really big arrays. From that perspective, it seems verbose to have to do add.apply(add, data) unless there was a real performance gain.