Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance of object (Array) encoding : #7

Merged
merged 1 commit into from
Feb 8, 2018

Conversation

PaulBlanche
Copy link
Contributor

Instead of a recursive approche for encoding/decoding which implies multiple calls to escape/unescape for nested values and ever-growing escaped strings, use another scheme

Encoding:

  1. Walk the nested array as if flattend, encode each primitive value (= non-array value) and escape them.
  2. Prefix the item with a K for each array opened directly before it, and suffix it with a ! for each array closed directly after it
  3. Join all items with a "
encode(['a', [['b']]]) // KJa"KKJb!!!

Encoding is done in a single pass on all items of the nested array.

Decoding:

  1. Split on ".
  2. Walk the flat array, keeping track of opened/closed array (counting prefix Ks and suffix !s), and push the unescaped and decoded value at the appropriate depth of the nestedarray
decode('KJb"KJa!"KJb!!') // ['b',['a'],['b']]

Decoding is also done in a single pass on all items

I also reworked the benchmark to use process.hrtime (more precise than Date) and to allow benchmarking of deep nested array vs flat arrays.

Instead of a recursive approche for encoding/decoding which implies
multiple calls to escape/unescape for nested values and ever-growing
escaped strings use another scheme :

Encoding:
1. Walk the nested array as if flattend, encode each primitive value
(= non-array value) and escape them.
2. Prefix the item with a `K` for each array opened directly before it,
and suffix it with a `!` for each array closed directly after it
3. Join all items with a `"`
```javascript
encode(['a', [['b']]]) // KJa"KKJb!!!
```
Encoding is done in a single pass on all items of the nested array.

Decoding:
1. Split on `"`.
2. Walk the flat array, keeping track of opened/closed array (counting
prefix `K`s and suffix `!`s), and push the unescaped and decoded value
at the appropriate depth of the nestedarray
```javascript
decode('KJb"KJa!"KJb!!') // ['b',['a'],['b']]
```
@dominictarr
Copy link
Owner

this is great work! now both encoding and decoding is faster than bytewise. merging!

@dominictarr dominictarr merged commit d8498fb into dominictarr:master Feb 8, 2018
@dominictarr
Copy link
Owner

merged into 3.0.0

@PaulBlanche PaulBlanche deleted the performance/object branch February 8, 2018 10:45
@PaulBlanche PaulBlanche mentioned this pull request Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants