From ae11f9e0c3444befb4bbb94d8ce0e1eb1c922ff7 Mon Sep 17 00:00:00 2001 From: xaviergonz Date: Fri, 28 Feb 2025 12:38:11 +0100 Subject: [PATCH] docs: enhance performance comparison for asMap and asSet functions in nodes documentation --- apps/site/docs/nodes.mdx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/apps/site/docs/nodes.mdx b/apps/site/docs/nodes.mdx index 721064c..129b6ad 100644 --- a/apps/site/docs/nodes.mdx +++ b/apps/site/docs/nodes.mdx @@ -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` 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` 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.