js 使用benchmark 测试代码性能
2018-04-10 00:00
316 查看
https://github.com/bestiejs/benchmark.js
测试代码和输出
测试js中,字符串的查找函数效率,分别使用正则表达式,includes,indexOf三个进行测试,结果表明indexOf最快
其中,Ops/sec 测试结果以每秒钟执行测试代码的次数(Ops/sec)显示,这个数值越大越好。除了这个结果外,同时会显示测试过程中的统计误差,以及相对最好的慢了多少(%)
测试代码和输出
测试js中,字符串的查找函数效率,分别使用正则表达式,includes,indexOf三个进行测试,结果表明indexOf最快
其中,Ops/sec 测试结果以每秒钟执行测试代码的次数(Ops/sec)显示,这个数值越大越好。除了这个结果外,同时会显示测试过程中的统计误差,以及相对最好的慢了多少(%)
var Benchmark = require('benchmark'); var suite = new Benchmark.Suite; // add tests suite.add('RegExp#test', function () { /o/.test('Hello World!'); }).add('includes#test', function () { 'Hello World!'.includes('o'); }).add('String#indexOf', function () { 'Hello World!'.indexOf('o') > -1; }) // add listeners .on('cycle', function (event) { console.log(String(event.target)); }) .on('complete', function () { console.log('Fastest is ' + this.filter('fastest').map('name')); }) // run async .run({'async': true}); // logs: // RegExp#test x 19,270,682 ops/sec ±1.39% (86 runs sampled) // includes#test x 40,055,049 ops/sec ±0.64% (90 runs sampled) // String#indexOf x 402,704,424 ops/sec ±2.44% (85 runs sampled) // Fastest is String#indexOf
相关文章推荐
- 使用Benchmark.js和jsPerf分析代码性能
- 测试当前IE浏览器文档模型版本的js代码(使用documenMode)
- 网站性能测试工具Apache Benchmark的使用说明
- 测试Swift语言代码高亮-使用highlight.js
- 你都使用哪些工具来测试代码的性能?
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- BizTalk性能测试(一)——BizTalk Benchmark Wizard 安装及使用
- 你都使用哪些工具来测试代码的性能?
- 使用console进行 性能测试 和 计算代码运行时间
- 你都使用哪些工具来测试代码的性能?
- 使用console进行 性能测试 和 计算代码运行时间(转载)
- 转载-网站性能测试工具Apache Benchmark的使用说明
- 你都使用哪些工具来测试代码的性能?
- 你都使用哪些工具来测试代码的性能?
- 使用Redis-benchmark为Redis性能测试
- 你都使用哪些工具来测试代码的性能?
- php 使用Benchmark对代码进行测试
- 你都使用哪些工具来测试代码的性能?
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 简单的代码片段性能测试 -- 顺便测一下正则和异常的性能(使用循环测试做比较)