Skip to content

Commit 500538a

Browse files
committed
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.
1 parent 4adb0c2 commit 500538a

4 files changed

Lines changed: 9 additions & 8 deletions

File tree

d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5624,8 +5624,8 @@
56245624
bs = b.substring(bi, bs);
56255625
if (s[i]) s[i] += bs; else s[++i] = bs;
56265626
}
5627-
if ((am = +am[0]) === (bm = +(bs = bm[0]))) {
5628-
if (s[i]) s[i] += bs; else s[++i] = bs;
5627+
if ((am = am[0]) === (bm = bm[0])) {
5628+
if (s[i]) s[i] += bm; else s[++i] = bm;
56295629
} else {
56305630
s[++i] = null;
56315631
q.push({

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interpolate/string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ function d3_interpolateString(a, b) {
2222
if (s[i]) s[i] += bs; // coalesce with previous string
2323
else s[++i] = bs;
2424
}
25-
if ((am = +am[0]) === (bm = +(bs = bm[0]))) { // coalesce matching numbers
26-
if (s[i]) s[i] += bs; // coalesce with previous string
27-
else s[++i] = bs;
25+
if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
26+
if (s[i]) s[i] += bm; // coalesce with previous string
27+
else s[++i] = bm;
2828
} else { // interpolate non-matching numbers
2929
s[++i] = null;
3030
q.push({i: i, x: d3_interpolateNumber(am, bm)});

test/interpolate/string-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ suite.addBatch({
4343
assert.strictEqual(interpolate("", "bar")(.5), "bar");
4444
assert.strictEqual(interpolate("", "")(.5), "");
4545
},
46-
"with two numerically-equivalent numbers, returns the target format": function(interpolate) {
47-
assert.strictEqual(interpolate("top: 1000px;", "top: 1e3px;")(.5), "top: 1e3px;");
46+
"with two numerically-equivalent numbers, returns the default format": function(interpolate) {
47+
assert.strictEqual(interpolate("top: 1000px;", "top: 1e3px;")(.5), "top: 1000px;");
48+
assert.strictEqual(interpolate("top: 1e3px;", "top: 1000px;")(.5), "top: 1000px;");
4849
}
4950
}
5051
});

0 commit comments

Comments
 (0)