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 5936391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:23:33+00:00 2026-05-22T15:23:33+00:00

For a function inside another function, does Lua instantiate the inner function on each

  • 0

For a function inside another function, does Lua “instantiate” the inner function on each call to the outer function? If so, would bar() in the code below perform worse than foo()?

local function a()
  print 'a'
end

function foo()
  a()
end

function bar()
  function b()
    print 'b'
  end

  b()
end
  • 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-05-22T15:23:33+00:00Added an answer on May 22, 2026 at 3:23 pm

    Test case 1: a and b both global, no embedding.

    $ cat junk.lua ; time lua junk.lua
    function a(n)
        return n + 1
    end
    
    function b(n)
        return a(n)
    end
    
    for c = 1, 10000000 do
        b(c)
    end
    
    
    real    0m1.743s
    user    0m1.740s
    sys 0m0.000s
    

    User time: 1.74s.

    Test case 2: a local, b global, no embedding.

    local function a(n)
        return n + 1
    end
    
    function b(n)
        return a(n)
    end
    
    for c = 1, 10000000 do
        b(c)
    end
    
    
    real    0m1.388s
    user    0m1.390s
    sys 0m0.000s
    

    User time 1.39s.

    Test case 3: a and b both local, no embedding.

    $ cat junk.lua ; time lua junk.lua
    local function a(n)
        return n + 1
    end
    
    local function b(n)
        return a(n)
    end
    
    for c = 1, 10000000 do
        b(c)
    end
    
    
    real    0m1.194s
    user    0m1.200s
    sys 0m0.000s
    

    User time 1.2s.

    Test case 4: a embedded in b, a global, b local.

    $ cat junk.lua ; time lua junk.lua
    local function b(n)
        function a(n)
            return n + 1
        end
    
        return a(n)
    end
    
    for c = 1, 10000000 do
        b(c)
    end
    
    
    real    0m2.804s
    user    0m2.790s
    sys 0m0.000s
    

    User time: 2.79s. (!)

    Test case 5: a embedded in b, both local.

    $ cat junk.lua ; time lua junk.lua
    local function b(n)
        local function a(n)
            return n + 1
        end
    
        return a(n)
    end
    
    for c = 1, 10000000 do
        b(c)
    end
    
    
    real    0m2.540s
    user    0m2.530s
    sys 0m0.000s
    

    User time: 2.53s.

    Result summary:

    1. It’s easy to write tests to confirm or deny intuitions about performance. You should probably do this instead of relying on crowd-sourcing the answer. (The crowd is often wrong, you see.)
    2. Making functions local instead of global has a significant positive impact on function call overhead. (About 30% better in this set of test cases when both functions were local.)
    3. Embedding a function in another function has a serious negative impact on function call overhead. (About 110% worse in this set of test cases when both functions were local.)
    4. Did I mention that testing is probably a good idea in lieux of trusting the crowd?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a function inside another function in python, but can't find
I have a function and I am trying to call that inside another function.
I need to use a function inside of another function, how can I do
I just realized a function may be defined inside another function in C: void
I was wondering if it was possible to call a function inside a Perl
I'm having trouble calling a specific function inside another on class. Example: var class
I have the code (inside one object) onclick: this._addX.bind(this) and then inside another object
I need help trying to call a class function within another class. Here's my
I have a function inside a class that returns a reference to a member
I've got a function inside of a class that returns a string. Inside this

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.