My JSPerf test case “breaks” while evaluating toggle() with callback

寵の児 提交于 2019-12-12 03:32:21

问题


While testing my answer to Adding HTML5 hidden attribute support to jQuery .toggle() method, I created a JSPerf test to determine how much slower .toggle(function() \[...\]) would be compared to .toggle(). To my amazement, the script with additional processing is reported to execute faster! Besides the results being counter-intuitive, I suspect a problem because I also see the toggling on the screen long after the results have returned.

How can I "fix" my test to get accurate results?


回答1:


I.e.

$('button').click(function() {
    $('#myElement').toggle(function() { alert("called") })
})

The callback function you pass to toggle only gets executed after the toggle action completes. In other words, it’s an asynchronous action, so if you want to measure how long it takes until the callback is fired, use jsPerf’s async/defer feature.



来源:https://stackoverflow.com/questions/14636333/my-jsperf-test-case-breaks-while-evaluating-toggle-with-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!