# Is calling hash() every time inefficient?

**URL:** <https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642>\
**Category:** Questions\
**Created:** [December 13, 2017, 3:02am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642 "2017-12-13T03:02:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelcurtis96](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@michaelcurtis96](https://forum.defold.com/u/michaelcurtis96)\
**Post date:** [December 13, 2017, 3:02am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/1 "2017-12-13T03:02:23Z")

</div>

I am learning how to pass messages upon input triggers and in the on\_input() function I am instructed to use the hash function to hash the action\_id i am targeting:

if action\_id == hash(“actionName”)

then do stuff…  
I am wondering if the hash function actually hashes the string in place or performs a table lookup  
If it is the former, then because the string is static I could just define the hashes as local constants and the hash function will only be called once.  
If it is the later then is the hash table lookup performed using binary search or something?

Just trying to make my game as efficient as possible. 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 13, 2017, 5:40am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/2 "2017-12-13T05:40:02Z")

</div>

You can pre-hash to save some performance.

local hashes = {}  
hashes.ACTION\_NAME = hash(“ACTION\_NAME”)

…  
if action\_id == hashes.ACTION\_NAME

A typical thing to do in a big project is to put your hashes into a single Lua module.

---

<div class="post-metadata">

**Author:** ![Insality](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/insality/32/39737_2.png) [@Insality](https://forum.defold.com/u/Insality)\
**Post date:** [December 13, 2017, 7:01am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/3 "2017-12-13T07:01:40Z")

</div>

I created for myself auto hash object:  
instead of hash(“string”) I’m using hashed.string

```lua
local _hashed = {}
hashed = {};
local mt = {
	__index = function(t, k)
		if _hashed[k] == nil then
			_hashed[k] = hash(k)
		end 
		return _hashed[k]
	end
}
setmetatable(hashed, mt)

```

---

<div class="post-metadata">

**Author:** ![sergey.lerg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sergey.lerg/32/3970_2.png) [@sergey.lerg](https://forum.defold.com/u/sergey.lerg)\
**Post date:** [December 13, 2017, 9:05am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/4 "2017-12-13T09:05:50Z")

</div>

I did the same thing.

```lua
local _M = {}

setmetatable(_M, {
	__index = function(t, key)
		local h = hash(key)
		rawset(t, key, h)
		return h
	end
})

return _M

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [December 13, 2017, 10:41am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/5 "2017-12-13T10:41:04Z")

</div>

> [@Insality](#):
>
> I created for myself auto hash object

That’s a nice solution, but wouldn’t it break code completion?

---

<div class="post-metadata">

**Author:** ![Insality](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/insality/32/39737_2.png) [@Insality](https://forum.defold.com/u/Insality)\
**Post date:** [December 13, 2017, 11:36am UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/6 "2017-12-13T11:36:10Z")

</div>

What does it means? Not fully understand you  
Yes, I have the global variable on the full project “hashed”, if you about this

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [December 13, 2017, 12:06pm UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/7 "2017-12-13T12:06:37Z")

</div>

Well, if you have something like this:

```
-- hashes.lua
local M = {}

M.TOUCH = hash("touch")

return M

-- foobar.script
local hashes = require "hashes"

function on_input(self, action_id, action)
    if action_id == hashes. <--- WHEN I PRESS '.' I'D GET CODE COMPLETION 

```

With you’re solution there’s no way to get code completion for the hashes since they’re not declared in the hashes.lua module right?

---

<div class="post-metadata">

**Author:** ![Insality](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/insality/32/39737_2.png) [@Insality](https://forum.defold.com/u/Insality)\
**Post date:** [December 13, 2017, 12:12pm UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/8 "2017-12-13T12:12:21Z")

</div>

Oh, now I am understand  
Sure this method without code completion (but I am using sublime, its help with it in some way)

PS. There is a way to show code completion window (hot-key) not only after press ‘.’ ?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [December 13, 2017, 1:03pm UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/9 "2017-12-13T13:03:46Z")

</div>

> [@Insality](#):
>
> PS. There is a way to show code completion window (hot-key) not only after press ‘.’ ?

I think it’s ctrl+space right?

---

<div class="post-metadata">

**Author:** ![Insality](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/insality/32/39737_2.png) [@Insality](https://forum.defold.com/u/Insality)\
**Post date:** [December 13, 2017, 1:26pm UTC](https://forum.defold.com/t/is-calling-hash-every-time-inefficient/14642/10 "2017-12-13T13:26:42Z")

</div>

Oh. I check this on windows, it’s work  
On my mac I have change language binding on ctrl+space, so it’s not working (it’s really painfull 😉 )  
Sorry for offtop
