Skip to content

Improve perf of fromCharCode(), fixes #71 #72

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

Merged
merged 1 commit into from
Aug 15, 2015
Merged
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
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,14 @@ function base64Slice (buf, start, end) {
}
}

function decodeCodePointsArray (array) {
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
// the lowest argument limit is Chrome, with 0x10000 args.
if (array.length < 0x10000) return String.fromCharCode.apply(String, array)
function decodeCodePointsArray (buf) {
var len = buf.length

if (len <= 0x10000) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might pull this constant out and put the explanation comment on it, that way its not magically here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, that's a good idea!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue isn't manual string concatenation; it's doing an unnecessary Array.slice() when the buffer.length is < 0x10000. I think it's pretty clear that doing slice() is slower than not doing slice(), although a JSPerf would tell us exactly how slow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return String.fromCharCode.apply(String, buf) // avoid extra slice()
}

// If above that limit, decode using string concatenation
// to avoid "call stack size exceeded".
return binarySlice(array, 0, array.length)
return binarySlice(buf, 0, len)
}

function utf8Slice (buf, start, end) {
Expand Down Expand Up @@ -723,13 +723,17 @@ function asciiSlice (buf, start, end) {
}

function binarySlice (buf, start, end) {
var ret = ''
end = Math.min(buf.length, end)
var chunk = 0x10000
var res = ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will re-add this, shouldn't have been removed.
Probably need to add a test for it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, my bad. sorry!


for (var i = start; i < end; i++) {
ret += String.fromCharCode(buf[i])
// Decode in chunks to avoid "call stack size exceeded".
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
// the lowest limit is Chrome, with 0x10000 args.
for (var i = start; i < end; i += chunk) {
var chunkEnd = Math.min(end, i + chunk)
res += String.fromCharCode.apply(String, buf.slice(i, chunkEnd))
}
return ret
return res
}

function hexSlice (buf, start, end) {
Expand Down