Skip to content

Instantly share code, notes, and snippets.

@yoya
Last active October 14, 2021 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoya/0755190bc800e4e8eba327ddcd395fbe to your computer and use it in GitHub Desktop.
Save yoya/0755190bc800e4e8eba327ddcd395fbe to your computer and use it in GitHub Desktop.
typedarray concat polyfill
(function() {
for (typedarr of [Int8Array, Uint8Array, Uint8ClampedArray,
Int16Array, Uint16Array, Int32Array, Uint32Array,
Float32Array, Float64Array]) {
if (!typedarr.prototype.concat) {
Object.defineProperty(typedarr.prototype, 'concat', {
value: function(b) {
const a = this;
if (!b || !b.length) return a.slice(0);
const c = new a.constructor(a.length + b.length);
c.set(a); c.set(b, a.length);
return c;
}
})
}
}
})();
const a = Uint8Array.from([11]);
const b = Int16Array.from([22, 33]);
const c = Float64Array.from([44, 55]);
const d = a.concat(c);
const e = b.concat(c);
console.log(a, b, c);
console.log(d, e);
@yoya
Copy link
Author

yoya commented Oct 13, 2021

% node typedarray_concat.js
Uint8Array(2) [ 11, 22 ] Uint16Array(3) [ 33, 44, 55 ]
Uint8Array(5) [ 11, 22, 33, 44, 55 ]

@yoya
Copy link
Author

yoya commented Oct 14, 2021

% node typedarray_concat.js
Uint8Array(1) [ 11 ] Int16Array(2) [ 22, 33 ] Float64Array(2) [ 44, 55 ]
Uint8Array(3) [ 11, 44, 55 ] Int16Array(4) [ 22, 33, 44, 55 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment