首页 > 解决方案 > 关于 RORO 花纹性能

问题描述

我很好奇在某些情况下使用 RORO 模式在内存消耗方面是邪恶的。

function findUsersByRole ({  role,   withContactInfo,   includeInactive}) {...}

代替

function findUsersByRole (  role,   withContactInfo,   includeInactive) {...}

很明显,当调用第一个函数时,会创建一个对象,并且该对象会占用内存中的空间。

我试图检查我的假设,但我没有得到任何明显的区别。看起来几乎一样。

这是我的代码使用@airbnb/node-memwatch


const
memwatch = require('@airbnb/node-memwatch');
url = require('url');

function roro({a, b}) {
        console.log(a, b);
}

function nonRoro(a, b) {
        console.log(a, b);
}

memwatch.gc();

function roroBenchmark() {
var hd = new memwatch.HeapDiff();

for (let i = 0; i < 10000; i++) {
        roro({a: 1, b: 2});
}

var hde = hd.end();

console.log(JSON.stringify(hde, null, 2));
}


function nonRoroBenchmark() {
var hd = new memwatch.HeapDiff();

for (let i = 0; i < 10000; i++) {
        nonRoro(1, 2);
}

var hde = hd.end();

console.log(JSON.stringify(hde, null, 2));
}


//roroBenchmark();
nonRoroBenchmark();

有什么建议可以验证我的假设吗?

标签: javascriptbenchmarking

解决方案


推荐阅读