扩展String.prototype性能表明函数调用快了10倍

exe*_*ook 6 javascript performance

我想用一些实用方法扩展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')
Run Code Online (Sandbox Code Playgroud)

结果:

func: 705 ms
proto: 10011 ms
proto-reuse: 10366 ms
proto-var: 9703 ms
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,差异是戏剧性的.

下面的证明方法的性能要求是neglectably慢,并且该功能码它自为方法慢.

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')
Run Code Online (Sandbox Code Playgroud)

结果:

func-dummy: 0.165ms
proto-dummy: 0.247ms
Run Code Online (Sandbox Code Playgroud)

虽然在大量重复(如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')
Run Code Online (Sandbox Code Playgroud)

结果:

func: 1689.942ms
method: 1674.639ms
Run Code Online (Sandbox Code Playgroud)

我一直在搜索Stackoverflow和binging,但其他建议"不要扩展字符串或数组,因为你污染了名称空间"(这对我的特定项目来说不是问题),我找不到任何与方法性能相关的东西与功能相比.因此,我应该忘记由于添加方法的性能下降而扩展String对象,还是有更多关于它的内容?

Ber*_*rgi 5

这很可能是因为您没有使用严格模式,并且this方法中的值需要强制转换为String实例而不是原始字符串.

您可以通过重复测量来确认这一点var STR = new String('01101011…').

然后修复你的实现:

String.prototype.count = function (char) {
    "use strict";
    var n = 0;
    for (var i = 0; i < this.length; i++)
        if (this[i] == char)
            n++;
    return n;
};
Run Code Online (Sandbox Code Playgroud)

  • @exebook:正如我所说,sloppy模式将`this`值转换为一个对象,所以它需要在每次调用时创建一个`String`实例,这对于像你这样简单的方法来说是一个很大的开销 (2认同)