Commit 500538af authored by Mike Bostock's avatar Mike Bostock
Browse files

Only coalesce exact string matches.

If there are a lot of matching numbers, it’s faster to do direct string equality
comparisons than it is to coerce to a number and compare numerically.
parent 4adb0c24
...@@ -5624,8 +5624,8 @@ ...@@ -5624,8 +5624,8 @@
bs = b.substring(bi, bs); bs = b.substring(bi, bs);
if (s[i]) s[i] += bs; else s[++i] = bs; if (s[i]) s[i] += bs; else s[++i] = bs;
} }
if ((am = +am[0]) === (bm = +(bs = bm[0]))) { if ((am = am[0]) === (bm = bm[0])) {
if (s[i]) s[i] += bs; else s[++i] = bs; if (s[i]) s[i] += bm; else s[++i] = bm;
} else { } else {
s[++i] = null; s[++i] = null;
q.push({ q.push({
......
This diff is collapsed.
...@@ -22,9 +22,9 @@ function d3_interpolateString(a, b) { ...@@ -22,9 +22,9 @@ function d3_interpolateString(a, b) {
if (s[i]) s[i] += bs; // coalesce with previous string if (s[i]) s[i] += bs; // coalesce with previous string
else s[++i] = bs; else s[++i] = bs;
} }
if ((am = +am[0]) === (bm = +(bs = bm[0]))) { // coalesce matching numbers if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
if (s[i]) s[i] += bs; // coalesce with previous string if (s[i]) s[i] += bm; // coalesce with previous string
else s[++i] = bs; else s[++i] = bm;
} else { // interpolate non-matching numbers } else { // interpolate non-matching numbers
s[++i] = null; s[++i] = null;
q.push({i: i, x: d3_interpolateNumber(am, bm)}); q.push({i: i, x: d3_interpolateNumber(am, bm)});
......
...@@ -43,8 +43,9 @@ suite.addBatch({ ...@@ -43,8 +43,9 @@ suite.addBatch({
assert.strictEqual(interpolate("", "bar")(.5), "bar"); assert.strictEqual(interpolate("", "bar")(.5), "bar");
assert.strictEqual(interpolate("", "")(.5), ""); assert.strictEqual(interpolate("", "")(.5), "");
}, },
"with two numerically-equivalent numbers, returns the target format": function(interpolate) { "with two numerically-equivalent numbers, returns the default format": function(interpolate) {
assert.strictEqual(interpolate("top: 1000px;", "top: 1e3px;")(.5), "top: 1e3px;"); assert.strictEqual(interpolate("top: 1000px;", "top: 1e3px;")(.5), "top: 1000px;");
assert.strictEqual(interpolate("top: 1e3px;", "top: 1000px;")(.5), "top: 1000px;");
} }
} }
}); });
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment