Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 14, 2015 02:39
Show Gist options
  • Save creationix/5015615 to your computer and use it in GitHub Desktop.
Save creationix/5015615 to your computer and use it in GitHub Desktop.
When working with typed arrays in node.js, make sure to give them custom inspect methods. Otherwise logging large values will blow up your console.
ArrayBuffer.prototype.inspect = function () {
return "<ArrayBuffer (0x" + this.byteLength.toString(16) + " bytes)>";
}
Uint8Array.prototype.inspect = function () {
var str = "<" + this.constructor.name;
for (var i = 0, l = Math.min(32, this.length); i < l; i++) {
str += " " + this[i].toString(16);
}
if (l < this.length) {
str += " (0x" + this.length.toString(16) + " total)";
}
return str + ">";
}
Int8Array.prototype.inspect = Uint8Array.prototype.inspect;
Uint16Array.prototype.inspect = Uint8Array.prototype.inspect;
Int16Array.prototype.inspect = Uint8Array.prototype.inspect;
Uint32Array.prototype.inspect = Uint8Array.prototype.inspect;
Int32Array.prototype.inspect = Uint8Array.prototype.inspect;
Float32Array.prototype.inspect = Uint8Array.prototype.inspect;
Float64Array.prototype.inspect = Uint8Array.prototype.inspect;
Object.defineProperty(ArrayBuffer.prototype, "length", {get: function () { return this.byteLength; }});
ArrayBuffer.prototype.inspect = Buffer.prototype.inspect;
Uint8Array.prototype.inspect = Buffer.prototype.inspect;
Int8Array.prototype.inspect = Buffer.prototype.inspect;
Uint16Array.prototype.inspect = Buffer.prototype.inspect;
Int16Array.prototype.inspect = Buffer.prototype.inspect;
Uint32Array.prototype.inspect = Buffer.prototype.inspect;
Int32Array.prototype.inspect = Buffer.prototype.inspect;
Float32Array.prototype.inspect = Buffer.prototype.inspect;
Float64Array.prototype.inspect = Buffer.prototype.inspect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment