-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
buffer: improve toJSON() performance #10895
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e4], | ||
len: [0, 10, 256, 4 * 1024] | ||
}); | ||
|
||
function main(conf) { | ||
var n = +conf.n; | ||
var buf = Buffer.allocUnsafe(+conf.len); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; ++i) | ||
buf.toJSON(); | ||
bench.end(n); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -804,10 +804,14 @@ Buffer.prototype.write = function(string, offset, length, encoding) { | |
|
||
|
||
Buffer.prototype.toJSON = function() { | ||
return { | ||
type: 'Buffer', | ||
data: Array.prototype.slice.call(this, 0) | ||
}; | ||
if (this.length) { | ||
const data = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this faster than using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is until appropriate backports to V8 5.4 are made. I've just decided to go ahead and use this method. See this issue for more information. |
||
for (var i = 0; i < this.length; ++i) | ||
data[i] = this[i]; | ||
return { type: 'Buffer', data }; | ||
} else { | ||
return { type: 'Buffer', data: [] }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the special case at all? The loop would run 0 times if length is 0, and you would get exactly the output you want. I doubt performance would be any different, would it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless the difference is really noticeable, I think I'd rather keep this simple and drop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a noticeable performance difference, otherwise I wouldn't have bothered. Without checking the IR explicitly, it probably has to do with setting up the loop or similar. |
||
} | ||
}; | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use
let
orconst
forn
andbuf
to maintain consistency across the codebaseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd rather just stick to
var
in benchmark code for the forseeable future. Having issues withlet
andconst
in node core code has left a bit of a "bad taste." While it might seem moot in benchmark code, I'd rather not have to worry about deopts making my benchmarks run unnecessarily longer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to what @mscdex said, the current position on
const
/let
overvar
is "only in code where performance doesn't matter". I'd say it makes more sense forbenchmarks/
to be consistent withlib/
thantest/
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh that was something new! Thanks!