我希望扩展String对象原型以添加一些实用方法。它可以工作,但性能却出奇的低。将字符串传递给函数比覆盖执行相同操作的String.prototype
方法快10倍。为了确保这一点,我创建了一个非常简单的count()
函数和相应的方法。
(我进行了实验,并创建了三个不同版本的方法。)
function count(str, char) {
var n = 0;
for (var i = 0; i < str.length; i++) if (str[i] == char) n++;
return n;
}
String.prototype.count = function (char) {
var n = 0;
for (var i = 0; i < this.length; i++) if (this[i] == char) n++;
return n;
}
String.prototype.count_reuse = function (char) {
return count(this, char)
}
String.prototype.count_var = function (char) {
var str = this;
var n = 0;
for (var i = 0; i < str.length; i++) if (str[i] == char) n++;
return n;
}
// Here is how I measued speed, using Node.js 6.1.0
var STR ='0110101110010110100111010011101010101111110001010110010101011101101010101010111111000';
var REP = 1e3//6;
console.time('func')
for (var i = 0; i < REP; i++) count(STR,'1')
console.timeEnd('func')
console.time('proto')
for (var i = 0; i < REP; i++) STR.count('1')
console.timeEnd('proto')
console.time('proto-reuse')
for (var i = 0; i < REP; i++) STR.count_reuse('1')
console.timeEnd('proto-reuse')
console.time('proto-var')
for (var i = 0; i < REP; i++) STR.count_var('1')
console.timeEnd('proto-var')
结果:
func: 705 ms
proto: 10011 ms
proto-reuse: 10366 ms
proto-var: 9703 ms
如您所见,差异巨大。
下面的证明显示方法调用的性能非常忽略不计,并且对于方法来说,函数代码本身的速度较慢。
function count_dummy(str, char) {
return 1234;
}
String.prototype.count_dummy = function (char) {
return 1234; // Just to prove that accessing the method is not the bottle-neck.
}
console.time('func-dummy')
for (var i = 0; i < REP; i++) count_dummy(STR,'1')
console.timeEnd('func-dummy')
console.time('proto-dummy')
for (var i = 0; i < REP; i++) STR.count_dummy('1')
console.timeEnd('proto-dummy')
console.time('func-dummy')
for (var i = 0; i < REP; i++) count_dummy(STR,'1')
console.timeEnd('func-dummy')
结果:
func-dummy: 0.165ms
proto-dummy: 0.247ms
虽然在大量重复操作(如1e8)时,原型方法的速度比函数慢10倍,但对于本案例可以忽略不计。
这一切可能只与String对象有关,因为当您将简单通用对象传递给函数或调用它们的方法时,它们的表现大致相同:
var A = { count: 1234 };
function getCount(obj) { return obj.count }
A.getCount = function() { return this.count }
console.time('func')
for (var i = 0; i < 1e9; i++) getCount(A)
console.timeEnd('func')
console.time('method')
for (var i = 0; i < 1e9; i++) A.getCount()
console.timeEnd('method')
结果:
func: 1689.942ms
method: 1674.639ms
我在Stackoverflow和Bing上搜索过,但除了建议“不要扩展String或Array因为它们会污染命名空间”(对于我的特定项目来说这不是问题)之外,我没有找到与方法性能相比较的函数相关的内容。所以我应该忽略由于添加方法而导致的性能下降而放弃扩展String对象呢?还是有更多需要考虑的呢?