Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize byte array encoding #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand All @@ -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) {
Expand Down