The map code will be slower because it's executing more JS, but both will take O(n) time and delegate the search itself to the interpreter so the map implementation isn't going to be that bad. Using indexOf is definitely slower than a map access because Espruino's object access can check the keys quickly at first rather than doing a full equality test, but it'd be interesting to do some benchmarks.
I'd definitely say use {} if you're at all able to though - it'll use less memory, is faster, and will probably get a lot better in future versions of Espruino too.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
How does
{}
compare to the map code above?The map code will be slower because it's executing more JS, but both will take
O(n)
time and delegate the search itself to the interpreter so the map implementation isn't going to be that bad. UsingindexOf
is definitely slower than a map access because Espruino's object access can check the keys quickly at first rather than doing a full equality test, but it'd be interesting to do some benchmarks.I'd definitely say use
{}
if you're at all able to though - it'll use less memory, is faster, and will probably get a lot better in future versions of Espruino too.