gpt4 book ai didi

javascript - javascript 类方法的调用比相同的函数慢 20-40 倍

转载 作者:行者123 更新时间:2023-12-03 07:05:36 29 4
gpt4 key购买 nike

在我的库中对代码进行基准测试时,我发现了非常有趣和令人失望的发现——看起来类方法的调用比相同的独立函数慢 20-40 倍。
简而言之,对我来说,这意味着 - “为了更好的性能 - 不要通过 JS 类实现非常频繁调用的功能”。通过“命名空间对象”访问库函数也有性能损失。该问题可能会影响设计库的方法。
我觉得我错过了什么。如果下面的性能测试有问题,请告诉我。
这是结果摘要,windows + intel 9600k (nodejs 14, Chrome 86, FF82)

          ops/s in:   node14 slow   Ch86 slow   FF82 slow
function(a, b) 4195 3934 1625
namespace.method(a, b) 210 x20 2702 x1.5 1305 x1.24
instance.method(a, b) 126 x33 162 x24 89 x18
prototype.method(a, b) 105 x40 154 x25 57 x28
class.method(a, b) 110 x38 153 x25 57 x28
https://benchmarkjs.com/ 的测试脚本,部分也复制/粘贴到 https://jsbench.me/ 以进行浏览器内测试。
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite("function-vs-method");
const nCycles = 1000000;

suite
.add('function(a, b)', function () {
function method(a, b) {
return a + b;
}

for( let i = 0; i < nCycles; i++ ) {
method(i, i)
}
})
.add('namespace.method(a, b)', function () {
function method(a, b) {
return a + b;
}

let namespace = {}
namespace.method = method

for( let i = 0; i < nCycles; i++ ) {
namespace.method(i, i)
}
})
.add('instance.method(a, b)', function () {
function method(a, b) {
return a + b;
}

function Test() {}
let instance = new Test()
instance.method = method

for( let i = 0; i < nCycles; i++ ) {
instance.method(i, i)
}
})
.add('prototype.method(a, b)', function () {
Test.prototype.method = (a, b) => {
return a + b;
}

function Test() {
}

let proto = new Test()
for( let i = 0; i < nCycles; i++ ) {
proto.method(i, i)
}
})
.add('class.method(a, b)', function () {
class Test {
method(a, b) {
return a + b;
}
}

let c = new Test()
for( let i = 0; i < nCycles; i++ ) {
c.method(i, i)
}
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();

最佳答案

您在测试中包含初始化,而不仅仅是比较方法/函数调用。
示例:您正在比较“声明和调用函数”与“声明类、实例化类对象和调用类方法”。
如果您在实际测试之外提取设置,结果看起来会大不相同。
此外,您的代码每次运行 nCycles for 循环产生了完全不同的结果。我将其更改为一次调用,现在可以在测试运行中看到持续的结果。
免责声明:到目前为止,我还没有做太多的基准测试,但想尽我所能来回答。 @everyone 请随时解释为什么我可能走错了路。 @op请不要盲目相信这个答案。

const Benchmark = require('benchmark');
const suite = new Benchmark.Suite('function-vs-method');

function method (a, b) {
return a + b;
}

let namespace = {};
namespace.method = function (a, b) {
return a + b;
};


function Test () {
}

let instance = new Test();
instance.method = function (a, b) {
return a + b;
};


TestProto.prototype.method = function (a, b) {
return a + b;
};

function TestProto () {
}

let proto = new TestProto();

class TestClass {
method (a, b) {
return a + b;
}
}

let c = new TestClass();

suite
.add('function(a, b)', function () {
method(1, 2);
})
.add('namespace.method(a, b)', function () {
namespace.method(1, 2);
})
.add('instance.method(a, b)', function () {
instance.method(1, 2);
})
.add('prototype.method(a, b)', function () {
proto.method(1, 2);
})
.add('class.method(a, b)', function () {
c.method(1, 2);
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
日志:
function(a, b) x 1,395,900,922 ops/sec ±0.11% (93 runs sampled)
namespace.method(a, b) x 1,397,715,256 ops/sec ±0.06% (98 runs sampled)
instance.method(a, b) x 1,397,494,031 ops/sec ±0.08% (96 runs sampled)
prototype.method(a, b) x 1,395,546,437 ops/sec ±0.09% (98 runs sampled)
class.method(a, b) x 1,398,311,922 ops/sec ±0.07% (96 runs sampled)
Fastest is class.method(a, b),namespace.method(a, b),instance.method(a, b),function(a, b)

关于javascript - javascript 类方法的调用比相同的函数慢 20-40 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65045683/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com