Skip to content

Commit

Permalink
Trading package size for float speed
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 5, 2016
1 parent 95c5538 commit 8a2dbc6
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
},
"globals": {
"Uint8Array": true,
"Float32Array": true,
"Float64Array": true,
"define": true,
"XMLHttpRequest": true,
"Promise": true
},
Expand Down
6 changes: 4 additions & 2 deletions bench/bench.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"sint32": -42
},
"outer" : {
"bool" : [ true, false, false, true, false, false, true ]
"bool" : [ true, false, false, true, false, false, true ],
"double": 0.5
}
}
},
"float": 0.25
}
2 changes: 2 additions & 0 deletions bench/bench.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ message Test {
string string = 1;
uint32 uint32 = 2;
Inner inner = 3;
float float = 4;

message Inner {

Expand Down Expand Up @@ -33,4 +34,5 @@ message Test {
message Outer {

repeated bool bool = 1;
double double = 2;
}
52 changes: 41 additions & 11 deletions bench/write.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,75 @@
var protobuf = require("../src/index"),
newSuite = require("./suite");

// This benchmark compares raw data type performance of Uint8Array and Buffer.
// This uses internal finish machinery because overall difference is otherwise mainly caused by allocation.

var array = new Uint8Array(8);
var buffer = new Buffer(8);

// The following is a possible alternative to float / double writing - where supported. Quite a bit faster.

var f32 = new Float32Array(1);
var i8 = new Uint8Array(f32.buffer);

function writeF32Array(buf, pos, val) {
f32[0] = val;
for (var i = 0; i < 4; ++i)
buf[pos + i] = i8[i];
}

// raw float write speed
newSuite("float")
.add("Writer#float", function() {
var writer = new protobuf.Writer();
writer.float(0.1);
writer.finish();
writer._finish(writer.head, array);
})
.add("Writer#writeF32Array", function() {
var writer = new protobuf.Writer();
writer.push(writeF32Array, 4, 0.1);
writer._finish(writer.head, array);
})
.add("BufferWriter#float", function() {
var writer = new protobuf.BufferWriter();
writer.float(0.1);
writer.finish();
writer._finish(writer.head, buffer);
})
.add("BufferWriter#writeF32Array", function() {
var writer = new protobuf.BufferWriter();
writer.push(writeF32Array, 4, 0.1);
writer._finish(writer.head, buffer);
})
.run();

// raw double write speed
newSuite("double")
.add("Writer#double", function() {
var writer = new protobuf.Writer();
writer.double(0.1);
writer.finish();
writer._finish(writer.head, array);
})
.add("BufferWriter#double", function() {
var writer = new protobuf.BufferWriter();
writer.double(0.1);
writer.finish();
writer._finish(writer.head, buffer);
})
.run();

var bytes = [0, 0, 0, 0, 0, 0, 0, 0];
var arrayBytes = new Uint8Array(bytes);
var bufferBytes = Buffer.from(bytes);
var arrayPlus1 = new Uint8Array(array.length + 1);
var bufferPlus1 = new Buffer(buffer.length + 1);

// raw bytes write speed
// interestingly, Uint8Array#set is faster than Buffer#copy, but only on actual Uint8Arrays.
newSuite("bytes")
.add("Writer#bytes", function() {
var writer = new protobuf.Writer();
writer.bytes(arrayBytes);
writer.finish();
writer.bytes(array);
writer._finish(writer.head, arrayPlus1);
})
.add("BufferWriter#bytes", function() {
var writer = new protobuf.BufferWriter();
writer.bytes(bufferBytes);
writer.finish();
writer.bytes(buffer);
writer._finish(writer.head, bufferPlus1);
})
.run();
151 changes: 115 additions & 36 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 8a2dbc6

Please sign in to comment.