# JavaScript Map

**URL:** <https://community.haxe.org/t/javascript-map/1840>\
**Category:** Haxe\
**Created:** [June 26, 2019, 9:58am UTC](https://community.haxe.org/t/javascript-map/1840 "2019-06-26T09:58:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrianV](https://community.haxe.org/user_avatar/community.haxe.org/adrianv/32/152_2.png) [@AdrianV](https://community.haxe.org/u/AdrianV)\
**Post date:** [June 26, 2019, 9:58am UTC](https://community.haxe.org/t/javascript-map/1840/1 "2019-06-26T09:58:04Z")

</div>

Hi,

every time I look at the generated js output I find the code for Map set and get very noisy. Shouldn’t it be possible to use:

```haxe
// JavaScript
h = Object.create(null);

```

instead of:

```haxe
h = {};

```

and test the reserved symbols with `__map_reserved` ?

the trick with `__map_reserved` shouldn’t be needed with an object without prototype, or am I missing something ?

---

<div class="post-metadata">

**Author:** ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)\
**Post date:** [June 26, 2019, 10:04am UTC](https://community.haxe.org/t/javascript-map/1840/2 "2019-06-26T10:04:06Z")

</div>

There is still an issue with ` __proto__ ` in some environments, see [[js] use Object.create(null) for StringMap when -D js-es5 · Issue #4372 · HaxeFoundation/haxe · GitHub](https://github.com/HaxeFoundation/haxe/issues/4372#issuecomment-160632623).

We might want to revisit this, because those run-times are probably now long obsolete, and looking up ` __proto__ ` could be considered an very rare edge-case anyway.

Another topic is that `StringMap.get/set` could be made non-inline not to spam the JS output, but I _think_ we also benchmarked that at some point and that caused actual slowdown. But again, maybe we should check this again.

---

<div class="post-metadata">

**Author:** ![AdrianV](https://community.haxe.org/user_avatar/community.haxe.org/adrianv/32/152_2.png) [@AdrianV](https://community.haxe.org/u/AdrianV)\
**Post date:** [June 26, 2019, 10:35am UTC](https://community.haxe.org/t/javascript-map/1840/3 "2019-06-26T10:35:13Z")

</div>

thanks for link. Didn’t know about the ` __proto__ ` problem.  
It is so weird we have `-D js-es=5` and `-D js-es=6` and the StringMap is still from the stone-age. At least for ES6 we should have a more elegant solution with JS Map.

---

<div class="post-metadata">

**Author:** ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)\
**Post date:** [June 26, 2019, 11:06am UTC](https://community.haxe.org/t/javascript-map/1840/4 "2019-06-26T11:06:48Z")

</div>

> [@AdrianV](#):
>
> At least for ES6 we should have a more elegant solution with JS Map.

This is yet another interesting discussion/research. We also did some benchmarks with native Map and found out that it was actually slower than the current implementation in all cases except some specific one.

I assume this is due to the fact ES6 Map guarantees element order, but this is something to research deeper.

Then again, I think there must be an option to make Haxe Maps use native ES6 Map, but I’m not sure if we should enable it by default with `js-es=6`.

---

<div class="post-metadata">

**Author:** ![mockey](https://community.haxe.org/user_avatar/community.haxe.org/mockey/32/612_2.png) [@mockey](https://community.haxe.org/u/mockey)\
**Post date:** [June 26, 2019, 2:22pm UTC](https://community.haxe.org/t/javascript-map/1840/5 "2019-06-26T14:22:21Z")

</div>

Still I would prefer to have the `inline` removed from `get`, `set`, `exists`, because that gets really noisy with inlined `__map_reserved`.  
I once created an issue:

> <https://github.com/HaxeFoundation/haxe/issues/5436>
>
> Looking at some generated JS, I was wondering if inlining the StringMap functions (as of 6b9e45f) on JS is really such...

but it was closed and inlinig (and code duplication) remained.

---

<div class="post-metadata">

**Author:** ![mark.knol](https://community.haxe.org/user_avatar/community.haxe.org/mark.knol/32/50_2.png) [@mark.knol](https://community.haxe.org/u/mark.knol)\
**Post date:** [June 26, 2019, 2:32pm UTC](https://community.haxe.org/t/javascript-map/1840/6 "2019-06-26T14:32:28Z")

</div>

I personally think how it performs is more important than how it looks, because in general the output is executed more than it is read by people.

But, in theory you can copy/paste haxe.ds.StringMap from [haxe/js/\_std/haxe/ds/StringMap](https://github.com/HaxeFoundation/haxe/blob/development/std/js/_std/haxe/ds/StringMap.hx) to your project using same structure and remove the inline if it bothers you that much. I haven’t tried this, but it should work.

---

<div class="post-metadata">

**Author:** ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)\
**Post date:** [June 26, 2019, 10:15pm UTC](https://community.haxe.org/t/javascript-map/1840/7 "2019-06-26T22:15:33Z")

</div>

> [@mockey](#):
>
> (and code duplication) remained

Well that’s not true, that issue was about the anonymous function duplication which was not really related to StringMap and was fixed.

---

<div class="post-metadata">

**Author:** ![AdrianV](https://community.haxe.org/user_avatar/community.haxe.org/adrianv/32/152_2.png) [@AdrianV](https://community.haxe.org/u/AdrianV)\
**Post date:** [June 27, 2019, 7:37am UTC](https://community.haxe.org/t/javascript-map/1840/8 "2019-06-27T07:37:09Z")

</div>

> [@nadako](#):
>
> Then again, I think there must be an option to make Haxe Maps use native ES6 Map, but I’m not sure if we should enable it by default with `js-es=6` .

Sounds like the same decision problem again and again. With JSES5 we don’t use Object.create(null) because not all ES5 browsers support it. With JSES6 we don’t use Map because not all ES6 browsers support it. Maybe it makes sense then to support Object.create(null) with `-D js-es=6` and maybe start to use Map with `-D js-es=6++`. If JS-Map has performance problems against our current StringMap, at least `Object.create(null)` should be faster (much ?) than current StringMap.

---

<div class="post-metadata">

**Author:** ![mockey](https://community.haxe.org/user_avatar/community.haxe.org/mockey/32/612_2.png) [@mockey](https://community.haxe.org/u/mockey)\
**Post date:** [June 28, 2019, 1:56pm UTC](https://community.haxe.org/t/javascript-map/1840/9 "2019-06-28T13:56:31Z")

</div>

> [@mark.knol](#):
>
> I personally think how it performs is more important than how it looks

I don’t care “how it looks”, but I don’t like bloated JS. As I wrote, removing `inline` decreased file size by 10%.

> [@mark.knol](#):
>
> But, in theory you can copy/paste haxe.ds.StringMap

Actually I do this, but it’s not a good solution in the long run. I constantly have to adapt that file to the Haxe version. And also overriding `haxe.ds` gets tricky when you use macros.

> [@nadako](#):
>
> that issue was about the anonymous function duplication which was not really related to StringMap and was fixed.

Oh, that’s right, sorry. I was looking at JS created with an older Haxe version. There is a temp var created now for anonymous functions, my bad.  
So I tested with current Haxe version and for one of my projects I get 96kB with `inline` vs 91.6kB with `inline` removed for `StringMap`. So it’s better now, but still like 5% difference (admittedly I used quite a lot of StringMaps there).

> [@AdrianV](#):
>
> Maybe it makes sense then to support Object.create(null)

Maybe I just don’t use `StringMap` anymore in JS but `Object.create(null)` with `DynamicAccess`. It’s supported in IE11, as far as I see, that’s the worst I need to support.
