2.4. Keys

In order to efficiently retrieve records stored in an indexed database, each record is organized according to its key.

A key has an associated type which is one of: number, date, string, binary, or array.

A key also has an associated value, which will be either: an [unrestricted double](https://heycam.github.io/webidl/#idl-unrestricted-double) if type is number or date, a [DOMString](https://heycam.github.io/webidl/#idl-DOMString) if type is string, a byte sequence if type is binary, or a list of other keys if type is array.

An ECMAScript [ECMA-262] value can be converted to a key by following the steps to convert a value to a key.

The following ECMAScript types are valid keys:

  • Number primitive values, except NaN. This includes Infinity and -Infinity.

  • Date objects, except where the [[DateValue]] internal slot is NaN.

  • String primitive values.

  • ArrayBuffer objects (or views on buffers such as Uint8Array).

  • Array objects, where every item is defined, is itself a valid key, and does not directly or indirectly contain itself. This includes empty arrays. Arrays can contain other arrays.

Attempting to convert other ECMAScript values to a key will fail.

An array key is a key with type array. The subkeys of an array key are the items of the array key‘s value.

To compare two keys a and b, run these steps:

  1. Let ta be the type of a.

  2. Let tb be the type of b.

  3. If ta does not equal tb, then run these steps:

    1. If ta is array, then return 1.

    2. If tb is array, then return -1.

    3. If ta is binary, then return 1.

    4. If tb is binary, then return -1.

    5. If ta is string, then return 1.

    6. If tb is string, then return -1.

    7. If ta is date, then return 1.

    8. Assert: tb is date.

    9. Return -1.

  4. Let va be the value of a.

  5. Let vb be the value of b.

  6. Switch on ta:

    number

    date

    1. If va is greater than vb, then return 1.

    2. If va is less than vb, then return -1.

    3. Return 0.

  1. *string*
  2. 1. If va is [code unit less than](https://infra.spec.whatwg.org/#code-unit-less-than) vb, then return -1.
  3. 2. If vb is [code unit less than](https://infra.spec.whatwg.org/#code-unit-less-than) va, then return 1.
  4. 3. Return 0.
  5. *binary*
  6. 1. If va is [byte less than](https://infra.spec.whatwg.org/#byte-less-than) vb, then return -1.
  7. 2. If vb is [byte less than](https://infra.spec.whatwg.org/#byte-less-than) va, then return 1.
  8. 3. Return 0.
  9. *array*
  10. 1. Let length be the lesser of vas [size](https://infra.spec.whatwg.org/#list-size) and vb’s [size](https://infra.spec.whatwg.org/#list-size).
  11. 2. Let i be 0.
  12. 3. While i is less than length, then:
  13. 1. Let c be the result of recursively running [compare two keys](#compare-two-keys) with va\[i\] and vb\[i\].
  14. 2. If c is not 0, return c.
  15. 3. Increase i by 1.
  16. 4. If vas [size](https://infra.spec.whatwg.org/#list-size) is greater than vb’s [size](https://infra.spec.whatwg.org/#list-size), then return 1.
  17. 5. If vas [size](https://infra.spec.whatwg.org/#list-size) is less than vb’s [size](https://infra.spec.whatwg.org/#list-size), then return -1.
  18. 6. Return 0.

The key a is greater than the key b if the result of running compare two keys with a and b is 1.

The key a is less than the key b if the result of running compare two keys with a and b is -1.

The key a is equal to the key b if the result of running compare two keys with a and b is 0.

As a result of the above rules, negative infinity is the lowest possible value for a key. Number keys are less than date keys. Date keys are less than string keys. String keys are less than binary keys. Binary keys are less than array keys. There is no highest possible key value. This is because an array of any candidate highest key followed by another key is even higher.

Members of binary keys are compared as unsigned byte values (in the range 0 to 255 inclusive) rather than signed [byte](https://heycam.github.io/webidl/#idl-byte) values (in the range -128 to 127 inclusive).