A TypeScript implementation of self-organizing lists with native Array methods.
npm install solistsA self-organizing list (or SoList) reorders elements based on access patterns to improve search efficiency. Frequently accessed items move toward the head, achieving near constant-time access in the best case.
Supported heuristics:
- Frequency Count - Accessed elements ordered by access count
- k-in-a-Row - Accessed element moves to front after k consecutive accesses
- Move-Ahead-k - Accessed element moves k positions toward the head
- Move to Front - Accessed element moves directly to the head
- Transpose - Accessed element swaps with its predecessor
By default (accessOnly: true), the list only rearranges on search operations. To also rearrange when adding elements, set accessOnly: false:
const l = new MoveToFrontSoList([1, 2, 3], { accessOnly: false });Methods that trigger rearrangement:
- Always:
at(),find(),findIndex(),findLast(),findLastIndex(),includes(),indexOf(),lastIndexOf() - When
accessOnly: false:push(),unshift(),insert()
Properties:
| Name | Description |
|---|---|
length |
Number of elements in the list |
Methods:
| Name | Description |
|---|---|
[Symbol.iterator]() |
Returns an iterator that yields each value |
at() |
Returns the value at a given index |
concat() |
Returns a new SoList merged with given iterables |
copyWithin() |
Shallow copies part of list to another location |
entries() |
Returns an iterator of key/value pairs |
every() |
Checks if every element satisfies a predicate |
fill() |
Changes all elements to a static value |
filter() |
Creates a new SoList with elements satisfying a predicate |
find() |
Returns the first element satisfying a predicate |
findIndex() |
Returns the index of the first element satisfying a predicate |
findLast() |
Returns the last element satisfying a predicate |
findLastIndex() |
Returns the index of the last element satisfying a predicate |
flat() |
Creates a new SoList with sub-lists flattened |
forEach() |
Executes a function for each element |
includes() |
Determines whether a value exists in the list |
indexOf() |
Returns the first index of a given element |
insert() |
Adds an element at a specific index |
isEmpty() |
Checks if the list contains no elements |
isEqual() |
Checks if the list equals a given iterable |
join() |
Joins all elements into a string |
keys() |
Returns an iterator of keys |
lastIndexOf() |
Returns the last index of a given element |
map() |
Creates a new SoList with mapped values |
pop() |
Removes and returns the last element |
push() |
Adds elements to the end |
reduce() |
Reduces values to a single value (left-to-right) |
reduceRight() |
Reduces values to a single value (right-to-left) |
remove() |
Removes the element at a specific index |
reverse() |
Reverses the order of elements |
shift() |
Removes and returns the first element |
slice() |
Returns a shallow copy of a portion |
some() |
Checks if any element satisfies a predicate |
sort() |
Sorts the elements |
splice() |
Adds/removes elements |
toLocaleString() |
Returns a localized string representation |
toReversed() |
Returns a reversed copy |
toSorted() |
Returns a sorted copy |
toSpliced() |
Returns a spliced copy |
toString() |
Returns a string representation |
unshift() |
Adds elements to the beginning |
values() |
Returns an iterator of values |
with() |
Returns copy with element at index replaced |
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. For contribution guidelines, see CONTRIBUTING.md.
The library is freely distributable under the terms of the MIT license.




