Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:34:29+00:00 2026-06-07T13:34:29+00:00

A friend of mine (who is much more skilled with javascript than I am)

  • 0

A friend of mine (who is much more skilled with javascript than I am) was helping me optimise some code recently, and one of the things that he mentioned might help was to call an external function instead of using a scoped function. However, it appears that this actually doesn’t appear to have much if any impact on performance. Would anyone who is more familiar with the inner workings of javascript mind explaining why this is the case? Is there any any reason to use one method over another?

What I mean is, this:

function showI(e) {
    var iVal = $(e).attr('iteration');
    var iValx99 = iVal * 99;
    var iValx999 = iVal * 999;
    alert(iVal + ' // ' + iValx99 + ' // ' + iValx999);
}
var element;
for (var i = 0; i < 50; i++) {
    element = $('<div />', {
        'class': 'iteration',
        'iteration': i
    });
    element.on('click', function() {
        showI(element);
    });
    element.appendTo('body');
}

versus:

var element;
for (var i = 0; i < 50; i++) {
    (function() {
        var j = i;
        element = $('<div />', {
            'class': 'iteration'
        });
        element.on('click', function() {
            var iVal = j;
            var iValx99 = iVal * 99;
            var iValx999 = iVal * 999;
            alert(iVal + ' // ' + iValx99 + ' // ' + iValx999);
        });
        element.appendTo('body');
    })();
}

jsperf benchmark example:

http://jsperf.com/function-click-on-every-element-vs-calling-a-function/2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:34:31+00:00Added an answer on June 7, 2026 at 1:34 pm

    Well, you are throwing away the performance gains from using a static function by defining a new function everytime anyway.

    What he probably meant was to use this:

    element.on('click', showI ); //Just passing an already created function is cheaper than creating a new function object everytime
    

    You don’t even need to bind it 50 times:

    $("body").on("click", ".iteration", showI );
    

    And modify showI:

    function showI(e) {
        var iVal = $(this).attr('iteration');
        var iValx99 = iVal * 99;
        var iValx999 = iVal * 999;
        alert(iVal + ' // ' + iValx99 + ' // ' + iValx999);
    }
    

    This is much faster in the modifed jsperf http://jsperf.com/function-click-on-every-element-vs-calling-a-function/3

    jsfiddle: http://jsfiddle.net/RgU5z/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a discussion recently with a friend of mine who is also a
I was recently asked by a friend of mine who's just starting to play
A friend of mine passed me a ShoutBox for an upgrade. Some users who
A friend of mine wrote some Google Closure code that contains an instance of
Recently I was talking with a friend of mine who had started a C++
A friend of mine sent me this code snippet to celebrate his new baby
Short story. This site was created by a friend of mine, who did not
I'm trying to help a friend of mine who has to do a test
Ok someone who happens to be a good friend of mine is sending me
This is for a friend of mine who is having trouble with Java for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.