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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:35:48+00:00 2026-06-11T17:35:48+00:00

i have a function in Lua that operates on a table, repeatedly adding entries

  • 0

i have a function in Lua that operates on a table, repeatedly adding entries to the table:

function DoStuff()
    local table = {};

    for i = 1, 1000 do
        local name, value = GetSomething(i);

        if (CheckSomething(name, value))
           table[name] = value
        end;
    end;
end;

i know this is fast; Lua has a good hash table algorithm. But now i need to split out part of my function, so i may unit test it:

function DoStuff()
    local table = {};

    for i = 1, 1000 do
        local name, value = GetSomething(i);

        --split out checking so it's testable
        table = ParseTheThing(table, name, value); 
    end;
end;

i know that Lua does not support passing paramters by reference, instead if i need to add an item to my table, Lua will make a copy and i must return that copy:

--Core checking function
function ParseTheThing(table, name, value)
   if (CheckSomething(name, value))
      table[name] = value
   end;

   return table;
end;

So it seems to me that every time i call CheckSomething Lua will create another table, that needs to be garbage collected.

Or maybe not. Maybe Lua does support passing parameters by reference, and i can simply call:

ParseTheThing(table, name, value); 

--Core checking function
function ParseTheThing(table, name, value)
   if (CheckSomething(name, value))
      table[name] = value
   end;
end;

Will Lua’s garbage collector be forced into doing a lot more work if i refactor my code as i have done?


Notes:

  • Don’t confuse the simplified example used in the question with the question i’m asking.
  • i don’t have any way to measure memory usage, or number of collections, so i cannot try it.
  • 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-11T17:35:49+00:00Added an answer on June 11, 2026 at 5:35 pm

    I know that Lua does not support passing paramters by reference

    The term “by reference” is overloaded, causing a lot of confusion (especially in the Java world where I’ve seen people refuse to acknowledge that one of the meanings even exists).

    Passing by reference can mean:

    1. Passing the location of an object in memory, rather than the object itself. This is what is meant by “pass by reference” in C. In this sense, Java does pass by reference, and so does Lua for many of its types (userdata, tables, etc.).

    2. An alias for a variable, such that any modifications to the alias is reflected in the original. This goes beyond simply not passing by value; if the variable holds a location of an object, you can assign a different location through the alias. You can’t do this for #1, where you pass a reference to an object, not a variable. These types of references are supported in C#, C++, etc., but not C or Lua.

    More simply:

    1. Pass by value: The object in the calling function is a copy of the object received by the called function.
    2. Pass by reference^1: The object in the calling function can be modified by the called function.
    3. Pass by reference^2: The variable in the calling function can be modified by the called function.

    instead if i need to add an item to my table

    Just pass the table — a reference, not a copy, is passed.

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

Sidebar

Related Questions

I have a lua table that I use as a hashmap, ie with string
I have a Lua table and an index function like this: curIndex = 0
I have a key => value table I'd like to sort in Lua. The
I have the following lua table : t = {name=sandy, age=22, major=computerScience} I want
I have a Lua function that, given n, generates all permutations of the series
I have a c function that is called from lua. The first parameter is
Is there a way to have a function in Lua that can be accessed
Say i have two files: One is called mainFile.lua: function altDoFile(name) dofile(debug.getinfo(1).source:sub(debug.getinfo(1).source:find(.*\\)):sub(2)..name) end altDoFile(libs/caller.lua)
I have a program that embeds Lua and implements a form of lazy function
I have function getCartItems in cart.js and I want to call that function in

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.