From 4b33dc6373767fc2e40658331dfaafdcd43bb074 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Wed, 17 Aug 2016 22:18:07 -0700 Subject: [PATCH 1/3] perf improvement: detect and use getTime() if scale(date) --- src/scale/financetime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scale/financetime.js b/src/scale/financetime.js index 3a54f1e9..e1f3e784 100644 --- a/src/scale/financetime.js +++ b/src/scale/financetime.js @@ -34,7 +34,7 @@ module.exports = function(d3_scale_linear, d3_time, d3_bisect, techan_util_rebin * @returns {*} */ function scale(x, offset) { - var mappedIndex = dateIndexMap[+x]; + var mappedIndex = dateIndexMap[x.getTime ? x.getTime() : +x]; offset = offset || 0; // Make sure the value has been mapped, if not, determine if it's just before, round in, or just after domain From 1c34333165ab03b5a7d300c512b27a37ba517f98 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Wed, 17 Aug 2016 22:40:33 -0700 Subject: [PATCH 2/3] perf improvement: avoid join() when possible in hot code paths --- src/plot/axisannotation.js | 24 ++++++------------------ src/plot/crosshair.js | 4 ++-- src/plot/macd.js | 8 ++------ src/plot/ohlc.js | 11 +++-------- src/plot/plot.js | 6 ++---- src/plot/tick.js | 10 ++-------- src/plot/volume.js | 8 ++------ 7 files changed, 19 insertions(+), 52 deletions(-) diff --git a/src/plot/axisannotation.js b/src/plot/axisannotation.js index f3873c74..d7f393d9 100644 --- a/src/plot/axisannotation.js +++ b/src/plot/axisannotation.js @@ -138,15 +138,9 @@ function backgroundPath(accessor, axis, height, width, point, neg) { if(height/2 < point) pt = height/2; else h = height/2-point; - return [ - 'M', 0, value, - 'l', neg*Math.max(axis.innerTickSize(), 1), -pt, - 'l', 0, -h, - 'l', neg*width, 0, - 'l', 0, height, - 'l', neg*-width, 0, - 'l', 0, -h - ].join(' '); + return 'M 0 ' + value + ' l ' + String(neg*Math.max(axis.innerTickSize(), 1)) + ' ' + String(-pt) + + ' l 0 ' + String(-h) + ' l ' + (neg*width) + ' 0 l 0 ' + height + + ' l ' + (neg*-width) + ' 0 l 0 ' + String(-h); case 'top': case 'bottom': var w = 0; @@ -154,15 +148,9 @@ function backgroundPath(accessor, axis, height, width, point, neg) { if(width/2 < point) pt = width/2; else w = width/2-point; - return [ - 'M', value, 0, - 'l', -pt, neg*Math.max(axis.innerTickSize(), 1), - 'l', -w, 0, - 'l', 0, neg*height, - 'l', width, 0, - 'l', 0, neg*-height, - 'l', -w, 0 - ].join(' '); + return 'M ' + value + ' 0 l' + String(-pt) + ' ' + (neg*Math.max(axis.innerTickSize(), 1)) + + ' l ' + String(-w) + ' 0 l 0 ' + String(neg*height) + ' l ' + width + ' 0 l 0 ' + (neg*-height) + + ' l ' + String(-w) + ' 0'; default: throw "Unsupported axis.orient() = " + axis.orient(); } }; diff --git a/src/plot/crosshair.js b/src/plot/crosshair.js index d7f2f708..4a906793 100644 --- a/src/plot/crosshair.js +++ b/src/plot/crosshair.js @@ -130,7 +130,7 @@ function horizontalPathLine(y, range) { return function(d) { if(d === null) return null; var value = y(d); - return ['M', range[0], value, 'L', range[range.length-1], value].join(' '); + return 'M ' + range[0] + ' ' + value + ' L ' + range[range.length-1] + ' ' + value; }; } @@ -140,6 +140,6 @@ function verticalPathLine(x, range) { var value = x(d), sr = x.range(); if(value < Math.min(sr[0], sr[sr.length-1]) || value > Math.max(sr[0], sr[sr.length-1])) return null; - return ['M', value, range[0], 'L', value, range[range.length-1]].join(' '); + return 'M ' + value + ' ' + range[0] + ' L ' + value + ' ' + range[range.length-1]; }; } \ No newline at end of file diff --git a/src/plot/macd.js b/src/plot/macd.js index 99590264..200ddad8 100644 --- a/src/plot/macd.js +++ b/src/plot/macd.js @@ -40,12 +40,8 @@ module.exports = function(accessor_macd, plot, plotMixin) { // Injected depende height = y(accessor.dif(d)) - zero, xValue = x(accessor.d(d)) - width/2; - return [ - 'M', xValue, zero, - 'l', 0, height, - 'l', width, 0, - 'l', 0, -height - ].join(' '); + return 'M ' + xValue + ' ' + zero + ' l 0 ' + height + ' l ' + width + + ' 0 l 0 ' + String(-height); }; } diff --git a/src/plot/ohlc.js b/src/plot/ohlc.js index 9f843d72..a8436998 100644 --- a/src/plot/ohlc.js +++ b/src/plot/ohlc.js @@ -36,14 +36,9 @@ module.exports = function(d3_scale_linear, d3_extent, accessor_ohlc, plot, plotM xPoint = x(accessor.d(d)), xValue = xPoint - width/2; - return [ - 'M', xValue, open, - 'l', width/2, 0, - 'M', xPoint, y(accessor.h(d)), - 'L', xPoint, y(accessor.l(d)), - 'M', xPoint, close, - 'l', width/2, 0 - ].join(' '); + return 'M ' + xValue + ' ' + + open + ' l ' + (width/2) + ' 0 M ' + xPoint + ' ' + y(accessor.h(d)) + ' L ' + + xPoint + ' ' + y(accessor.l(d)) + ' M ' + xPoint + ' ' + close + ' l ' + (width/2) + ' 0'; }; } diff --git a/src/plot/plot.js b/src/plot/plot.js index 315221db..97252072 100644 --- a/src/plot/plot.js +++ b/src/plot/plot.js @@ -114,10 +114,8 @@ module.exports = function(d3_svg_line, d3_select) { var firstDatum = d[0], lastDatum = d[d.length-1]; - return [ - 'M', x(accessor_date(firstDatum)), y(accessor_value(firstDatum)), - 'L', x(accessor_date(lastDatum)), y(accessor_value(lastDatum)) - ].join(' '); + return 'M ' + x(accessor_date(firstDatum)) + ' ' + y(accessor_value(firstDatum)) + + ' L ' + x(accessor_date(lastDatum)) + ' ' + y(accessor_value(lastDatum)); }; }, diff --git a/src/plot/tick.js b/src/plot/tick.js index 57f233dc..ba9e431e 100644 --- a/src/plot/tick.js +++ b/src/plot/tick.js @@ -35,14 +35,8 @@ module.exports = function(d3_scale_linear, d3_extent, accessor_tick, plot, plotM xPoint = x(accessor.d(d)), xValue = xPoint - width/2; - return [ - 'M', xValue, high, - 'l', width, 0, - 'M', xPoint, high, - 'L', xPoint, low, - 'M', xValue, low, - 'l', width, 0 - ].join(' '); + return 'M ' + xValue + ' ' + high + ' l ' + width + ' 0 M ' + xPoint + ' ' + high + + ' L ' + xPoint + ' ' + low + ' M ' + xValue + ' ' + low + ' l ' + width + ' 0'; }; } diff --git a/src/plot/volume.js b/src/plot/volume.js index 4dfe9c22..9e9026e7 100644 --- a/src/plot/volume.js +++ b/src/plot/volume.js @@ -37,12 +37,8 @@ module.exports = function(accessor_volume, plot, plotMixin) { // Injected depen height = y(vol) - zero, xValue = x(accessor.d(d)) - width/2; - return [ - 'M', xValue, zero, - 'l', 0, height, - 'l', width, 0, - 'l', 0, -height - ].join(' '); + return 'M ' + xValue + ' ' + zero + ' l 0 ' + height + ' l ' + width + + ' 0 l 0 ' + String(-height); }; } From bb527164fc33c73ab02d6f7a4e1f2a6772a2ab05 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Wed, 17 Aug 2016 23:13:54 -0700 Subject: [PATCH 3/3] fix bad path in backgroundPath --- src/plot/axisannotation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plot/axisannotation.js b/src/plot/axisannotation.js index d7f393d9..8bfca5e6 100644 --- a/src/plot/axisannotation.js +++ b/src/plot/axisannotation.js @@ -148,7 +148,7 @@ function backgroundPath(accessor, axis, height, width, point, neg) { if(width/2 < point) pt = width/2; else w = width/2-point; - return 'M ' + value + ' 0 l' + String(-pt) + ' ' + (neg*Math.max(axis.innerTickSize(), 1)) + + return 'M ' + value + ' 0 l ' + String(-pt) + ' ' + (neg*Math.max(axis.innerTickSize(), 1)) + ' l ' + String(-w) + ' 0 l 0 ' + String(neg*height) + ' l ' + width + ' 0 l 0 ' + (neg*-height) + ' l ' + String(-w) + ' 0'; default: throw "Unsupported axis.orient() = " + axis.orient();