Slow performance in V8 functions added to String.prototype?

I used some code to extract unsigned 16-bit values ​​from a string.

I found that adding this function to the prototype for String:

String.prototype.UInt16 = function(n) {
    return this.charCodeAt(n) + 256 * this.charCodeAt(n + 1);
};

much slower than just a function that takes a parameter Stringas a parameter:

var UInt16 = function(s, n) {
    return s.charCodeAt(n) + 256 * s.charCodeAt(n + 1);
};

In Firefox, the difference is only twice, but in Chrome 15 it is a hundred times slower!

See the results http://jsperf.com/string-to-uint16

Can someone clarify this and / or suggest an alternative way to use the prototype without a performance hit?

+5
source share
2

( ) , .

http://jsperf.com/string-to-uint16/2

10 2x firefox .

? , . , .

+1

Alnitak, jsperf test ( ), , . , , V8, , Java - .

0

All Articles