什么更快?运行一个空函数或检查函数是否未定义? [英] What is faster? Running an empty function or checking if function is undefined?

查看:124
本文介绍了什么更快?运行一个空函数或检查函数是否未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,其中一个作为参数传入的函数有时可能是未定义的。对这种糟糕的实践感到好奇,我想知道实际上速度更快?给一个空函数,或者让函数检查参数是否未定义?



我做了以下测试来尝试。答案非常令人惊讶!

  var timesTest = 1000; 

函数empty(){}
console.time('运行一个空函数');
for(var i = 0; i< timesTest; i ++){
empty();
}
console.timeEnd('运行一个空函数');

var somethingthatdoesnotexist;
console.time('检查函数是否存在');
for(var i = 0; i< timesTest; i ++){
if(somethingthatnoteesxist){
somethingthatdoesnotexist();
}
}
console.timeEnd('检查函数是否存在');

//结果:
//运行一个空函数:0.103ms
//检查函数是否存在:0.036ms

在数字较小的情况下,检查未定义参数的速度要快得多。



一旦测试时间增加,事情就会变得有趣。

  // var timesTest = 100000; 
//结果:
//运行一个空函数:1.125ms
//检查函数是否存在:1.276ms

  //结果:
// var timesTest = 1000000000;
//运行一个空函数:2096.941ms
//检查函数是否存在:2452.922ms

随着测试数量的增长,运行空白函数的速度变得更快一些。



我还没有试图在图上绘制这个图,但我对这种行为很好奇。有人知道为什么吗?如何影响现实世界中的代码? http://jsperf.com ,以获得更准确的基准和花哨图表。我做了一个: http://jsperf.com/empty-vs-check


  • 这是微型优化。 NOBODY会注意到差异。 10亿次迭代的差异不到半秒,这绝不会发生。做你认为更可读的东西;不要担心表演。



  • I was writing some code where a function being passed in as an argument might sometimes be undefined. Being curious about this as bad 'practice', I wondered what is actually faster? Giving an empty function, or having the function check if the argument was undefined?

    I did the following test to try. The answer was very surprising!

    var timesTest = 1000;
    
    function empty(){}
    console.time('running an empty function');
    for( var i=0; i<timesTest; i++ ){
      empty();
    }
    console.timeEnd('running an empty function');
    
    var somethingthatdoesnotexist;
    console.time('checking if a function exists');
    for( var i=0; i<timesTest; i++ ){
      if( somethingthatdoesnotexist ){
        somethingthatdoesnotexist();
      }
    }
    console.timeEnd('checking if a function exists');
    
    // results:
    // running an empty function: 0.103ms
    // checking if a function exists: 0.036ms
    

    At low numbers, checking for undefined argument is much faster.

    Things get interesting once the times tested increases.

    // var timesTest = 100000;
    // results:
    // running an empty function: 1.125ms
    // checking if a function exists: 1.276ms 
    

    and

    // results:
    // var timesTest = 1000000000;
    // running an empty function: 2096.941ms
    // checking if a function exists: 2452.922ms 
    

    As the number of tests grew, running a blank function becomes a bit faster by a margin.

    I haven't tried plotting this out on a graph yet, but I'm curious as to this behavior. Does anyone know why this is? How does this affect things in real world code?

    解决方案

    1. http://jsperf.com for more accurate benchmarking and fancy graphs. I made one: http://jsperf.com/empty-vs-check

    2. This is micro-optimization. NOBODY WILL EVER NOTICE A DIFFERENCE. There's a difference of less than a half a second for a billion iterations, which will never happen. Do what you think is more readable; don't worry about performance.

    这篇关于什么更快?运行一个空函数或检查函数是否未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆