Monday, March 16, 2026

Javascript: Just use eval

So we want to show in the interface the number of online players, so we need to "bind" a div to the value nested in an object:

<span data-bind="network.players.length"></span>

So we implement a function that with a given path and a root/global object as a starting point we get the value. But actually we want the length + 1 because that list does not include the current player... now what?

Should implement arithmetic operations, should we create a getter for length_plus1, should we pre-calculate the value, should we do any of that and cache it? No.

We just use the best function that a interpreted language can offer: eval.

But eval has security problems you say, it has no security problems its just perfectly fine, the only security problem is the developer. But it could cause performance problems, it can but will make it a provider/producer function and it will be perfectly fine:

provider = eval("() => " + expression)  // once
element.innerText = provider()          // forever

And if you think about this is even faster than the property lookup path parser version because it has now been "compiled" into a function that does no parsing at all.



done_