0.3.1 • Published 12 years ago

speedr v0.3.1

Weekly downloads
14
License
-
Repository
github
Last release
12 years ago

Speedr.js

Improved javascript objects.

Javascript objects work well as maps in many cases, but there are big gaps in their implementation when used as such. For example:

  • What if I need sorting?
  • Do I really need to loop through and count manually in order to get the number of elements I've defined in an object?
  • What If I need fast iteration on Node.js or Chrome? For small objects, speed might not be an issue, but as an object grows in size, iteration speeds can become troublesome.

Speedr addresses these issues and then some by introducing a new Map class.

  • Map An unsorted hash map that can only store one value per key (i.e. keys are unique*).
  • SortedMap * A hash map that sorts its keys upon insertion.
  • SortedMultiMap * Sorted map that allows multiple entries to be inserted under the same key.
// From a native javascript object
var map = new Map({a: 1, b:2});

// Or by passing a series of arrays as [key, value] pairs
var map = new Map(['a', 1], ['b', 2]);

The reason for this is that object literal keys are always strings. i.e. the key in {1: 'a'} is not the number 1, but the string '1'. Thus, if you want to preserve your numbered keys, you must use the new Map([1, 'a'], [2, 'b']) style of construction rather than passing in an object.