美文网首页
JS代码段的基准测试

JS代码段的基准测试

作者: Mica_马超 | 来源:发表于2022-01-06 17:41 被阅读0次

    window.performance.now()

      const s = window.performance.now()
    
      this.$refs.table.toggleRowSelection(row)
    
       setTimeout(() => {
         this.renderTime = (window.performance.now() - s).toFixed(2) + 'ms'
       })
    

    console.time方法与console.timeEnd方法

    console.time('small loop');
    for (var i = 0; i < 100000; i++) {
        ;
    }
    console.timeEnd('small loop');
    
    console.time(1);
    setTimeout(function(){
        console.timeEnd(1);
    }, 16.7);
    

    Benchmark.js

    var suite = new Benchmark.Suite;
    
    // add tests
    suite.add('RegExp#test', function() {
      /o/.test('Hello World!');
    })
    .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 4,161,532 +-0.99% (59 cycles)
    // => String#indexOf x 6,139,623 +-1.00% (131 cycles)
    // => Fastest is String#indexOf
    

    相关文章

      网友评论

          本文标题:JS代码段的基准测试

          本文链接:https://www.haomeiwen.com/subject/cmjrcrtx.html