A library boasting about its performance would be incomplete without a benchmark. It is unwise to judge a library solely by microbenchmarking, however. Microbenchmark results are easily inaccurate, inapplicable, or even manipulated, so take the following data with a grain of salt.
It is advised that you conduct tests in your own realistic use cases if performance is of concern. And if have done so, whether you have chosen this library or not in the end, it would be kind of you to share your results on GitHub.
The source files for all benchmarks on this page end with extension .bench.ts.
The results are presented in the same format as the JS web framework benchmark.
Measurements are made from the median of 10 repetitions.
Because all of the libraries are implemented in pure-JavaScript, their performance depends directly on the JavaScript engine. Test data are only gathered for V8 because that is what powers Node.js, but keep in mind that on other engines they will perform differently.
Queries on npm have resulted in the following list of competing implementations.
Array (array, set; control group):
A naïve implementation backed by an ordinary Array.
Searching is done by bisecting; insertion and deletion is done with Array.prototype.splice.
It checks for duplicates at insertion time to emulate a set.Set and Map (set, map; control group):
A naïve implementation backed by ordinary Set and Map.
Keys are sorted on demand.
Note that Set and Map does not support custom comparators and is thus limited to primitive keys.TreeMultiSet, TreeSet and TreeMap (array, set, map):
A red-black tree that provides an API in C++ STL style.OrderedSet and OrderedMap (set, map):SortedSet and SortedMap (set, map):
A venerable collection library in the ES5 age.
The SortedSet and SortedMap are backed by an splay tree implementation.RBTree (set):
A minimalistic red-black tree.Several alternative implementations were omitted for reasons documented below:
bisect module from Python.While sorted-containers allows random access on all three types of containers, many of the alternatives are implemented with various kinds of balanced binary search trees, and have not considered random access (indexing) abilities. They are omitted from the corresponding benchmarks.
Even those that provide methods to look up values by index do it awfully slowly, as slow as traversing the whole tree to get to a single node. functional-red-black-tree is a notable one that has fast random access.
sorted-containers uses a nested Array data structure similar to a B-tree limited to two levels. sorted-btree is the only library based on B-tree in the list. It is also the only one that delivers performance on par with sorted-containers.
A B-tree-like data structure have a load factor parameter that can have a significant impact on performance. The default values are used in the benchmarks, but there may be trade-offs for different workloads. They should be fine tuned based on the exact usage in case needed. Thus stock benchmarks for different load factors are not provided.
The benchmarks are run on GitHub-hosted Actions runner ubuntu-latest as of ???.