From 1a6de156903322c39734c613def5f19e00d1a934 Mon Sep 17 00:00:00 2001 From: Matt Vollrath Date: Wed, 17 Oct 2018 01:21:43 -0400 Subject: [PATCH] Optimize byte array encoding --- cbor.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cbor.js b/cbor.js index 3e1f300..c53e43a 100644 --- a/cbor.js +++ b/cbor.js @@ -30,6 +30,7 @@ var POW_2_24 = 5.960464477539063e-8, function encode(value) { var data = new ArrayBuffer(256); var dataView = new DataView(data); + var byteView = new Uint8Array(data); var lastLength; var offset = 0; @@ -42,6 +43,7 @@ function encode(value) { var oldDataView = dataView; data = new ArrayBuffer(newByteLength); dataView = new DataView(data); + byteView = new Uint8Array(data); var uint32count = (offset + 3) >> 2; for (var i = 0; i < uint32count; ++i) dataView.setUint32(i << 2, oldDataView.getUint32(i << 2)); @@ -60,9 +62,8 @@ function encode(value) { commitWrite(prepareWrite(1).setUint8(offset, value)); } function writeUint8Array(value) { - var dataView = prepareWrite(value.length); - for (var i = 0; i < value.length; ++i) - dataView.setUint8(offset + i, value[i]); + prepareWrite(value.length); + byteView.set(value, offset); commitWrite(); } function writeUint16(value) {