Skip to content

Commit 844e24c

Browse files
committed
AJ's suggestion to memorize imaginary tickval defaults
1 parent 3380c0d commit 844e24c

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/plots/smith/layout_defaults.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ var layoutAttributes = require('./layout_attributes');
1616
var constants = require('./constants');
1717
var axisNames = constants.axisNames;
1818

19+
var makeImagDflt = memoize(function(realTickvals) {
20+
return realTickvals.slice().reverse().map(function(x) { return -x; })
21+
.concat([0])
22+
.concat(realTickvals);
23+
}, String);
24+
1925
function handleDefaults(contIn, contOut, coerce, opts) {
2026
var bgColor = coerce('bgcolor');
2127
opts.bgColor = Color.combine(bgColor, opts.paper_bgcolor);
@@ -55,11 +61,10 @@ function handleDefaults(contIn, contOut, coerce, opts) {
5561
if(isRealAxis) {
5662
coerceAxis('tickvals');
5763
} else {
58-
var realTickvals = contOut.realaxis.tickvals || layoutAttributes.realaxis.tickvals.dflt;
59-
var imagTickvalsDflt =
60-
realTickvals.slice().reverse().map(function(x) { return -x; })
61-
.concat([0])
62-
.concat(realTickvals);
64+
var imagTickvalsDflt = makeImagDflt(
65+
contOut.realaxis.tickvals ||
66+
layoutAttributes.realaxis.tickvals.dflt
67+
);
6368

6469
coerceAxis('tickvals', imagTickvalsDflt);
6570
}
@@ -132,3 +137,15 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
132137
layoutOut: layoutOut
133138
});
134139
};
140+
141+
function memoize(fn, keyFn) {
142+
var cache = {};
143+
return function(val) {
144+
var newKey = keyFn ? keyFn(val) : val;
145+
if(newKey in cache) { return cache[newKey]; }
146+
147+
var out = fn(val);
148+
cache[newKey] = out;
149+
return out;
150+
};
151+
}

0 commit comments

Comments
 (0)