问题标签 [jsperf]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
209 浏览

javascript - 为什么当数组大小低于 3-5000 时,自定义拼接函数的性能优于原生拼接?

我正在为游戏编写 A* 寻路的实现,并试图找到将值插入整数数组的最快方法。我制作了一个自定义拼接功能,并针对本机进行了测试。

如果数组大小低于大约 3-5000,则自定义函数优于原生函数(在 chrome 上)。结果对我很有用,因为我可以根据数组大小选择最佳函数,但我不明白为什么 native 在较大的数组上更好,而在较小的数组上自定义。你能解释一下为什么吗?

测试在这里: https ://jsperf.com/native-splice-vs-custom

自定义拼接功能:

0 投票
1 回答
65 浏览

javascript - 为什么 Array.prototype.push 比变量声明快

我运行了以下 jsperf https://jsperf.com/push-vs-define-anaoum

并发现在这种情况下推送:

比仅仅用里面的子数组声明 a 更快:

谁能告诉我为什么或指出我正确的文档?

谢谢 在此处输入图像描述

0 投票
2 回答
5233 浏览

javascript - Faster loop: foreach vs some (performance of jsperf is different than node or chrome)

Which is the best way to resume the value of an array into simple true or false values.

I am quite confused as jsperf is giving me VERY different results than what google chrome console, nodejs, or any other JS engine gives me. (jsperf snippet here)

Bad jsperf performance Image

This is the code snippet, and you can see (you can run it here) that some is like 100 times faster than using a foreach loop

The question is, Why is JSPERF giving different results than chrome's console, nodejs, or any other JS engine?

EDIT: As stated by my answer to the question underneath, the behaviour was buggy, because I had the chrome dev tools open when using JSPERF, and all of the messages were being logged to the console, which means that actually the results changed. Keep in mind for the future, that JSPERF might not behave properly when leaving the console open on execution.

0 投票
0 回答
707 浏览

javascript - 如何在 js 项目中使用复杂性报告工具

我在一个 javascript 项目上安装了复杂性报告

安装是使用项目根目录上的$ npm install complex-report完成的。我可以运行$cr,它会显示一些配置选项,如上图所示

我不知道我必须使用哪个命令或我必须遵循哪些步骤来计算例如特定 js 文件夹的复杂性?

我需要一些帮助来描述如何使用这个工具。此处给出了对该工具的一些解释,但我不明白在我的情况下下一步该做什么。

提前致谢

0 投票
0 回答
75 浏览

javascript - 为什么硬编码开关在受限的浅拷贝中表现更好?

我正在开发一个反应自定义渲染器,我必须将反应道具浅复制到后端元素,其中有一些保留属性,如“key”、“ref”、“children”,我应该过滤掉,尤其是“children”与元素的“children”属性冲突,Object.assign 不能这样做。我不需要复制未知属性,只需要复制一个重要的受限集。

我发现了一些奇怪的东西,与 Object.assign 相比,在迭代中硬编码 switch 语句可以在大多数现代浏览器上获得更好的性能,我发现与手写代码相比,使用生成的代码构建“函数”可以获得更好的性能(我不确定,它的性能与我重构代码中的手写代码相同)。

我创建了几个测试用例:

https://jsperf.com/copy-known-properties-few

https://jsperf.com/copy-known-properties-many/4

https://jsperf.com/copy-known-properties-polymorphic

第二个包括一个额外的构造函数案例。

这里也是一个重构版本测试,更容易阅读:

https://stackblitz.com/edit/limited-assign-perf

我想知道为什么以及这是否可行,或者是否有更好的方法。

我的第一个问题,谢谢。