Skip to content

Commit 590dd49

Browse files
committed
Use slice instead of creating a new buffer
1 parent 7ab3255 commit 590dd49

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ function deFramer(onFrame) {
117117
case 1: length |= chunk[i] << 16; state = 2; break;
118118
case 2: length |= chunk[i] << 8; state = 3; break;
119119
case 3: length |= chunk[i]; state = 4;
120-
buffer = bops.create(length);
121120
offset = 0;
122121
break;
123122
case 4:
@@ -127,9 +126,14 @@ function deFramer(onFrame) {
127126
emit = true;
128127
len = length - offset;
129128
}
130-
// TODO: optimize for case where a copy isn't needed can a slice can
131-
// be used instead?
132-
bops.copy(chunk, buffer, offset, i, i + len);
129+
if (emit && offset === 0) {
130+
buffer = bops.subarray(chunk, i, i + len);
131+
} else if (offset === 0) {
132+
buffer = bops.create(length);
133+
bops.copy(chunk, buffer, 0, i, i + len);
134+
} else {
135+
bops.copy(chunk, buffer, offset, i, i + len);
136+
}
133137
offset += len;
134138
i += len - 1;
135139
if (emit) {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "snappy-msgpack-stream",
3-
"version": "1.0.0",
3+
"version": "1.0.4",
44
"description": "Streams of framed Snappy-compressed MessagePack messages",
55
"main": "index.js",
66
"scripts": {
@@ -14,7 +14,7 @@
1414
},
1515
"keywords": [
1616
"snappy",
17-
"json",
17+
"msgpack",
1818
"stream",
1919
"pipe",
2020
"frame"
@@ -26,6 +26,6 @@
2626
},
2727
"repository": {
2828
"type": "git",
29-
"url": "https://github.com/KlausTrainer/snappy-json-stream.git"
29+
"url": "https://github.com/KlausTrainer/snappy-msgpack-stream.git"
3030
}
3131
}

readme.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# snappy-msgpack-stream
22

3-
[![Build Status](https://travis-ci.org/KlausTrainer/snappy-msgpack-stream.svg?branch=master)](https://travis-ci.org/KlausTrainer/snappy-msgpack-stream)
3+
[![Build Status](https://travis-ci.org/KlausTrainer/snappy-msgpack-stream.svg?branch=main)](https://travis-ci.org/KlausTrainer/snappy-msgpack-stream)
44

55
Streams of framed [Snappy](https://google.github.io/snappy/)-compressed [MessagePack](http://msgpack.org/) messages..
66

0 commit comments

Comments
 (0)