Skip to content

Commit

Permalink
docs: enhance performance comparison for asMap and asSet functions in…
Browse files Browse the repository at this point in the history
… nodes documentation
  • Loading branch information
xaviergonz committed Feb 28, 2025
1 parent 489d14a commit ae11f9e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions apps/site/docs/nodes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -606,18 +606,27 @@ Note that, when cloning, all node keys are replaced with new unique keys.

### `asMap`

The `asMap` function converts a plain object, an observable object or an object node into a `Map<string, value>` view that can be used to interact with the object as if it were a Map.
The `asMap` function converts a plain object, an observable object or an object node into a `Map<string, value>` view that can be used to interact with the object as if it were a Map. Operations should be as fast as with a real Map:


| Operation | Map | asMap (observable object) | asMap (plain object) |
|-----------|------|---------------------------|----------------------|
| add | O(1) | O(1) | O(1) |
| delete | O(1) | O(1) | O(1) |
| has | O(1) | O(1) | O(1) |
| clear | O(n) | O(n) | O(n) |
| iteration | O(n) | O(n) | O(n) |

### `asSet`

The `asSet` function converts a plain array, an observable array or an array node into a Set-like view that can be used to interact with the array as if it were a Set. Note that since the backing store **for plain arrays** is an array some operations will be slower than with a real set or asSet with an observable array, namely:

| Operation | Set / asSet (observable array) | asSet (plain array) |
|-----------|--------------------------------|---------------------|
| add | O(1) | O(n) |
| delete | O(1) | O(n) |
| has | O(1) | O(n) |
| clear | O(n) | O(n) |
| iteration | O(n) | O(n) |
| Operation | Set | asSet (observable array) | asSet (plain array) |
|-----------|------|--------------------------|---------------------|
| add | O(1) | O(1) | O(n) |
| delete | O(1) | O(n) | O(n) |
| has | O(1) | O(1) | O(n) |
| clear | O(n) | O(n) | O(n) |
| iteration | O(n) | O(n) | O(n) |

If speed is paramount and your values are strings consider using `asMap` with a record that saves true when the value exists and delete when it does not.

0 comments on commit ae11f9e

Please sign in to comment.