0

From the docs at https://redis.io/docs/stack/json/path/#legacy-path-syntax:

The time complexity of searching (navigating to) an element in the path is calculated from:

Child level - every level along the path adds an additional search

Key search - O(N)†, where N is the number of keys in the parent object

Array search - O(1)

If I have json like:

{
   "inventory": {
      "headphones": [
         {
            "id": 12345,
            "name": "Noise-cancelling Bluetooth headphones",
            "description": "Wireless Bluetooth headphones with noise-cancelling technology",
            "wireless": true,
            "connection": "Bluetooth",
            "price": 99.98,
            "stock": 25,
            "free-shipping": false,
            "colors": ["black", "silver"]
         },
         {
            "id": 12346,
            "name": "Wireless earbuds",
            "description": "Wireless Bluetooth in-ear headphones",
            "wireless": true,
            "connection": "Bluetooth",
            "price": 64.99,
            "stock": 17,
            "free-shipping": false,
            "colors": ["black", "white"]
         },
         {
            "id": 12347,
            "name": "Mic headset",
            "description": "Headset with built-in microphone",
            "wireless": false,
            "connection": "USB",
            "price": 35.01,
            "stock": 28,
            "free-shipping": false
         }
      ],
      "keyboards": [
         {
            "id": 22345,
            "name": "Wireless keyboard",
            "description": "Wireless Bluetooth keyboard",
            "wireless": true,
            "connection": "Bluetooth",
            "price": 44.99,
            "stock": 23,
            "free-shipping": false,
            "colors": ["black", "silver"]
         },
         {
            "id": 22346,
            "name": "USB-C keyboard",
            "description": "Wired USB-C keyboard",
            "wireless": false,
            "connection": "USB-C",
            "price": 29.99,
            "stock": 30,
            "free-shipping": false
         }
      ]
   }
}

and do

JSON.GET obj $.inventory.headphones

what is the time complexity of this? Is it O(1) to access .inventory, O(1) to access headphones, and then O(1) to return value here (the entire array of headphone objects)? which would sum to O(1)?

Or is it O(N1) to search through all the keys of the parent (N1=1), another O(N2) to search through the first child (in this case N2=2 for headphone and keyboard), and then O(M * N3) where N3=9 to search through all the keys in the object and M=3 for the number of objects in the array? which would sum to O(N1 + N2 + M*N3)?

Stevie
  • 326
  • 3
  • 16

0 Answers0