Locally scoped functions

Benchmark created by Dwayne Charrington on


Description

Showing the performance differences between locally scoped functions.

Setup

var foodsArr = ['pizza', 'banana', 'pie', 'steak', 'fries', 'soup', 'tuna', 'rice', 'greek salad', 'bagels'];
    
    function consumeFoods1(foods) {
        for (var i = 0, len = foods.length; i < len; i++) {
            eat(foods[i]);
        }
    }
    
    function consumeFoods2(foods) {
        var eatFunc = eat;
        for (var i = 0, len = foods.length; i < len; i++) {
            eatFunc(foods[i]);
        }
    }
    
    function eat(food) {
        console.log('You just ate: '+food+'');
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Not locally scoped
consumeFoods1(foodsArr);
ready
Locally scoped
consumeFoods2(foodsArr);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Dwayne Charrington on