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

  • Home
  • SEARCH
  • 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 9191077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:35:58+00:00 2026-06-17T20:35:58+00:00

obj = [1,2,3,4,5]; function iter(){ for (var key in obj){ key=key+key; }; }; function

  • 0
obj = [1,2,3,4,5];
function iter(){
    for (var key in obj){
        key=key+key;
    };
};
function test1() { 
    iter(obj); 
};
function test2(){
    (function iter(obj){
        for (var key in obj){
            key=key+key;
        };
    })(obj);    
};

Here, both test1 and test2 perform the same, even though test2 is supposedly creating a new function everytime it is called. Why?

  • 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-17T20:35:59+00:00Added an answer on June 17, 2026 at 8:35 pm

    My guess is that there’s no difference in performance because there’s no (meaningful) difference in the code. The parser creates the local iter function inside test2 once when it parses the code, not each time test2 is called. (This isn’t like using eval.) If anything, the second one will be a tiny bit faster because obj is local to the iter function. Well, that was wrong.

    As this jsperf test shows, the second is indeed slower. You have to be careful about measurement. The way you wrote the functions, the amount of work being done in the function bodies easily masks the difference in function call overhead involved in the two cases. Also, the first case is accessing a global obj, while the second is accessing an argument. These differences should be eliminated to, as much as possible, measure only what you’re trying to measure. The jsperf test I wrote tries to do just that.

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

Sidebar

Related Questions

var Bar = function(){ } Bar.prototype.drawBar = function(obj){ } var bars = [];// Creating
So there is this function Array.prototype.containsCaseInsensitive = function(obj) { var i = this.length; while
I have this code: var obj = function (i) { this.a = i; this.init
For the following JavaScript: function EscapedString(str) { var obj = document.createElement(span); obj.innerHTML = str;
How can I create a function that can't be called from outside? var obj
I have this piece of code: this.json.each(function(obj, index) { var li = new Element('li');
var DynamicModelView = { createModelView: function (obj,vitalslength,headerValue) { for(vitalsCount = 0, vitalsLen = vitalslength;
My Code is Simple var core ={ f: function(obj){ console.log(toString.call(obj)); } } Which should
I have seen in Javascript , var obj = { } function addMember(memberName,value) {
If i have: var obj={ a:function(){obj.b();}, b:function(){this.a();} }; is there any difference in calling

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.