Skip to content

Commit

Permalink
[PoC] bypass ax.d2c for typedArray during ax.makeCalcdata
Browse files Browse the repository at this point in the history
... and use typed array 'subarray' to slice coordinate arrays
    to series length
  • Loading branch information
etpinard committed Feb 20, 2018
1 parent a25ff13 commit 45c2f35
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ lib.relinkPrivateKeys = require('./relink_private');
lib.ensureArray = require('./ensure_array');

var isArrayModule = require('./is_array');
lib.isArrayOrTypedArray = isArrayModule.isArrayOrTypedArray
lib.isTypedArray = isArrayModule.isTypedArray;
lib.isArrayOrTypedArray = isArrayModule.isArrayOrTypedArray;

var coerceModule = require('./coerce');
lib.valObjectMeta = coerceModule.valObjectMeta;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/is_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ?
{isView: function() { return false; }} :
ArrayBuffer;

exports.isTypedArray = ab.isView;

exports.isArrayOrTypedArray = function(a) {
return Array.isArray(a) || ab.isView(a);
};
11 changes: 8 additions & 3 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,15 @@ module.exports = function setConvert(ax, fullLayout) {
if(axLetter in trace) {
arrayIn = trace[axLetter];
len = trace._length || arrayIn.length;
arrayOut = new Array(len);

for(i = 0; i < len; i++) {
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal);
if(ax.type === 'linear' && Lib.isTypedArray(arrayIn) && arrayIn.subarray) {
arrayOut = arrayIn.subarray(0, len);
} else {
arrayOut = new Array(len);

for(i = 0; i < len; i++) {
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal);
}
}
}
else {
Expand Down
42 changes: 42 additions & 0 deletions test/jasmine/tests/is_array_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,45 @@ describe('isArrayOrTypedArray', function() {
});
});
});

describe('isTypedArray', function() {
function A() {}

var shouldPass = [
new Float32Array(1),
new Int32Array([1, 2, 3])
];

var shouldFail = [
new Array(10),
[],
A,
new A(),
document,
window,
null,
undefined,
'string',
true,
false,
NaN,
Infinity,
/foo/,
'\n',
new Date(),
new RegExp('foo'),
new String('string')
];

shouldPass.forEach(function(obj) {
it('treats ' + JSON.stringify(obj) + ' as an array', function() {
expect(Lib.isTypedArray(obj)).toBe(true);
});
});

shouldFail.forEach(function(obj) {
it('treats ' + JSON.stringify(obj !== window ? obj : 'window') + ' as NOT an array', function() {
expect(Lib.isTypedArray(obj)).toBe(false);
});
});
});

0 comments on commit 45c2f35

Please sign in to comment.