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?
source
share