-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buffer.byteLength is called whenever a new string Buffer is created. UTF8 is used as the default encoding, and base64 is also popular. These must be fast and take up a relatively significant part of Buffer instantiation. This commit moves the Buffer.byteLength calculations into only JS-land, moving it from C++ land for base64 and UTF8. It also adds a benchmark for both encodings; the improvements hover around 40-60% for UTF8 strings and 170% for base64.
- Loading branch information
1 parent
0a48a8b
commit cb04c10
Showing
4 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var common = require('../common'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
encoding: ['utf8', 'base64'], | ||
len: [4, 16, 64, 256, 1024], | ||
n: [1e7] | ||
}); | ||
|
||
// 16 chars of each byte length | ||
var encodings = { | ||
'utf8': { | ||
0: 'hello brendan!!!', // 1 byte | ||
1: 'ΰαβγδεζηθικλμνξο', // 2 bytes | ||
2: '挰挱挲挳挴挵挶挷挸挹挺挻挼挽挾挿', // 3 bytes | ||
3: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢' // 4 bytes | ||
}, | ||
'base64': { | ||
0: 'aGVsbG8gd29ybGQh', // no padding | ||
1: 'YW55IGNhcm5hbCBwbGVhc3VyZS4=', // one byte | ||
2: 'YWJjZGVmZ2hpamtsbW5eYw==', // two bytes | ||
3: 'YXN1cmUu', // nada | ||
} | ||
}; | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
var len = conf.len | 0; | ||
var encoding = conf.encoding; | ||
var chars = encodings[encoding]; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i++) { | ||
Buffer.byteLength(chars[n % 4], encoding); | ||
} | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters