Aln*_*tak 5 javascript performance google-chrome v8
我一直在使用一些代码从字符串中提取无符号的16位值.
我发现将此函数添加到原型中String:
String.prototype.UInt16 = function(n) {
return this.charCodeAt(n) + 256 * this.charCodeAt(n + 1);
};
Run Code Online (Sandbox Code Playgroud)
比只有一个函数String作为一个参数慢得多:
var UInt16 = function(s, n) {
return s.charCodeAt(n) + 256 * s.charCodeAt(n + 1);
};
Run Code Online (Sandbox Code Playgroud)
在Firefox中,差异仅为两倍,但在Chrome 15中它的速度要慢一百倍!
请参阅http://jsperf.com/string-to-uint16上的结果
任何人都可以为此提供解释,和/或提供使用原型的替代方法,而不会影响性能吗?
从原语访问原型(因为它不是对象)比从对象访问原型慢得多。
http://jsperf.com/string-to-uint16/2
对我来说,Chrome 中的速度快了 10 倍,Firefox 中的速度快了 2 倍。
使用原型是否存在实际瓶颈?如果您不需要每秒数百万次操作,它仍然非常快。如果需要,只需使用一个函数即可。