Skip to content

Commit 0ef69ee

Browse files
committed
make bins faster by quantization
ref. observablehq/plot#454
1 parent 7c62a55 commit 0ef69ee

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/bin.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function() {
1818
var i,
1919
n = data.length,
2020
x,
21+
step,
2122
values = new Array(n);
2223

2324
for (i = 0; i < n; ++i) {
@@ -45,7 +46,7 @@ export default function() {
4546
// compare order (>=) rather than strict equality (===)!
4647
if (tz[tz.length - 1] >= x1) {
4748
if (max >= x1 && domain === extent) {
48-
const step = tickIncrement(x0, x1, tn);
49+
step = tickIncrement(x0, x1, tn);
4950
if (isFinite(step)) {
5051
if (step > 0) {
5152
x1 = (Math.floor(x1 / step) + 1) * step;
@@ -75,10 +76,20 @@ export default function() {
7576
}
7677

7778
// Assign data to bins by value, ignoring any outside the domain.
78-
for (i = 0; i < n; ++i) {
79-
x = values[i];
80-
if (x != null && x0 <= x && x <= x1) {
81-
bins[bisect(tz, x, 0, m)].push(data[i]);
79+
if (step && isFinite(step)) {
80+
step = step < 0 ? -step : 1 / step;
81+
for (i = 0; i < n; ++i) {
82+
x = values[i];
83+
if (x != null && x0 <= x && x <= x1) {
84+
bins[Math.floor((x - x0) * step)].push(data[i]);
85+
}
86+
}
87+
} else {
88+
for (i = 0; i < n; ++i) {
89+
x = values[i];
90+
if (x != null && x0 <= x && x <= x1) {
91+
bins[bisect(tz, x, 0, m)].push(data[i]);
92+
}
8293
}
8394
}
8495

0 commit comments

Comments
 (0)