-
Notifications
You must be signed in to change notification settings - Fork 171
Improve array decoding performance #32
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
Conversation
For large arrays performance difference can be even bigger and also it will produce less trash for GC
842279c
to
3d50366
Compare
codecov-io
commented
May 27, 2019
Codecov Report
@@ Coverage Diff @@ ## master #32 +/- ## ========================================== + Coverage 96.14% 96.14% +<.01% ========================================== Files 14 14 Lines 752 753 +1 Branches 158 158 ========================================== + Hits 723 724 +1 Misses 12 12 Partials 17 17
Continue to review full report at Codecov.
|
Tested with this benchmark script:
/* eslint-disable no-console */ import { encode, decode } from "../src"; // @ts-ignore import _ from "lodash"; const data = _.cloneDeep(new Array(1000).fill([[1], [1, 2, 3], [1, 2, 3, 4, 5]])); // warm up const encoded = encode(data); decode(encoded); // run console.time("encode"); for (let i = 0; i < 10000; i++) { encode(data); } console.timeEnd("encode"); console.time("decode"); for (let i = 0; i < 10000; i++) { decode(encoded); } console.timeEnd("decode");
And this PR significantly improves decoding:
before:
$ npx ts-node benchmark/sandbox.ts
encode: 2753.986ms
decode: 1855.263ms
after:
$ npx ts-node benchmark/sandbox.ts
encode: 2393.308ms
decode: 3804.347ms
Great!
(CI failed because of SauceLabs credentials; will fix it later https://docs.travis-ci.com/user/environment-variables/ )
nelsonlaquet
commented
Jul 2, 2019
I wonder if you could improve the performance further on certain engines by allocating an array without holes in it:
array: Array.from({ length: size }),
Source:
https://v8.dev/blog/elements-kinds
https://2ality.com/2018/12/creating-arrays.html
Tried it, but Array.from({ length: size })
makes decoding 10x slower than new Array(size)
:-(
Hi @gfx I've used this msgpack library in my project and found small optimization that improves decoding speed by 3-4%.