最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - High performance JS map for int-string pairs - Stack Overflow

programmeradmin3浏览0评论

I need a high performance map in Javascript (hashmap or whatever) that maps int(s) to string(s). The map would be used to build some sections of the webpage after dom is ready. I know simple javascript object also works like a map but I need to work with best performance.

I would like to initialize the map with all data pairs just at once by appending a string to the webpage while generating the response page from server.

Any ways how to improve performance of javascript map for int- string pairs or are there any implementations for the same ?

--

Using jQuery 1.7

I need a high performance map in Javascript (hashmap or whatever) that maps int(s) to string(s). The map would be used to build some sections of the webpage after dom is ready. I know simple javascript object also works like a map but I need to work with best performance.

I would like to initialize the map with all data pairs just at once by appending a string to the webpage while generating the response page from server.

Any ways how to improve performance of javascript map for int- string pairs or are there any implementations for the same ?

--

Using jQuery 1.7

Share Improve this question edited Jan 14, 2015 at 12:55 Jonik 81.8k76 gold badges271 silver badges379 bronze badges asked Oct 25, 2012 at 9:56 Rajat GuptaRajat Gupta 26.6k65 gold badges187 silver badges298 bronze badges 6
  • Use an array. Taken into account that any implementation will have to use js primitives and objects, you'll be hard pressed to find something more performant than that. – soulcheck Commented Oct 25, 2012 at 10:10
  • also arrays in most js implementations switch automatically to sparse implementation if needed – soulcheck Commented Oct 25, 2012 at 10:18
  • @soulcheck: array would be great but the integer keys might range from 0 to Integer.MAX_VALUE(32 bit). So if, for e.g., I store something in an array like this array1[23465435], would it create such a big length array !? I need to store not more than 100-200 entries but the int key used might be scattered over the entire Integer range. – Rajat Gupta Commented Oct 25, 2012 at 12:31
  • also ints might be even negative at times. In that case would array be helpful ? – Rajat Gupta Commented Oct 25, 2012 at 12:32
  • arrays in most (all?) implementations of javascript can be sparse. So array.length will return the index of last element + 1, but in sparse case the array will not have all elements allocated and will use object property semantics to access it's elements (meaning, it's effectively a hashtable with ints as keys). It basically gives you the behavior you're looking for. In case of negative ints, use a second array ;) – soulcheck Commented Oct 25, 2012 at 12:36
 |  Show 1 more ment

3 Answers 3

Reset to default 12

Ok, I'll post it here since it's more of an answer:

Use an array. Taken into account that any implementation will have to use js primitives and objects, you'll be hard pressed to find something more performant than that.

Arrays in most (all?) implementations of javascript can be sparse. So array.length will return the index of last element + 1, but in sparse case the array will not have all elements allocated and will use object property semantics to access it's elements (meaning, it's effectively a hashtable with ints as keys).

It basically gives you the behavior you're looking for.

In case of negative ints, use a second array.

In relation to a single statement initialization: you can't do it in general, since it bases itself on implicitly knowing the item index.

What you can do is to append something along the lines:

 var arr = [];

 arr[int1] = val1;
 arr[int2] = val2;
 arr[int3] = val3;
 arr[int4] = val4;
 ...
 arr[intn] = valn;

I mean you have to list (Number, String) pairs somehow anyway.

I think you should use the following var l_map = {};

to add an element use l_map[<your integer>] = <your string>

and to retrieve is var l_value = l_map[<your integer>];

This is one way to solve your problem.

The second way is quite simple just use an array (or list) because it stores the values based on position as follows: var l_array = [];

to add element at the last use : l_array.push(<your string>); to add element at a specified position : l_array.splice(<position>,0,<your string>);

and to retrieve use : l_array[<posit>];

Please check out this jperf test case, and draw your conclusion. Objects are also sparse. Arrays are simply specialized objects that account for their own length among other things.

发布评论

评论列表(0)

  1. 暂无评论