vega / vega-transforms

Data processing transforms for Vega dataflows.
BSD 3-Clause "New" or "Revised" License
2 stars 6 forks source link

Support summing strings as numbers. #9

Closed domoritz closed 6 years ago

domoritz commented 6 years ago

Vega aggregates support strings instead of numbers for all aggregates (as far as I can see) except sum. This is because JavaScript treats + as concatenation as soon as one argument is a string (🤦‍♂️). See https://beta.observablehq.com/@domoritz/wtf-js-numbers for examples.

This PR converts strings to numbers for sum aggregates.

domoritz commented 6 years ago

Unless I'm missing something, JavaScript already does it for -.

domoritz commented 6 years ago

As for timing, here is a benchmark of how slow repeated coercion is compared to working with raw numbers: https://beta.observablehq.com/@domoritz/timing-number-coercion.

My conclusions are that working with raw numbers is faster than strings but the differences are small because the jit seems to optimize away most of the differences. Also, summing a few numbers is already super fast and the overhead of coercing strings to numbers is small (i.e. probably not noticeable). This means its probably okay to not add parse for numbers in Vega-Lite.

domoritz commented 6 years ago

cc @saba9

domoritz commented 6 years ago

This issue is blocking some refactoring in Vega-Lite and it would be nice if we could release this.