Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bench (name, iterations = 500, fn) {
- const t0 = performance.now();
- for (let i = 0; i < iterations; i++) fn(i)
- let d = performance.now() - t0;
- d = Math.ceil(d);
- let ops = iterations / d;
- ops = Math.ceil(ops);
- ops = new Intl.NumberFormat('en').format(ops);
- console.log(`${name} took: ~%f ms or ~%s ops/ms`, d, ops)
- }
- obj = {
- abcd0: "foo0",
- abcd1: "foo1",
- abcd2: "foo2",
- abcd3: "fo03",
- };
- // Number of times we run the tests
- iters = 10 ** 7;
- console.clear();
- // Normal variable assignments of obj props
- bench(`assign object vars`, iters, function (i){
- var abcd0 = obj.abcd0;
- var abcd1 = obj.abcd1;
- var abcd2 = obj.abcd2;
- var abcd3 = obj.abcd3;
- return abcd0 + abcd1 + abcd2 + abcd3 + i;
- });
- // By destructuring
- bench(`destruct object vars`, iters, function (i){
- var { abcd0, abcd1, abcd2, abcd3 } = obj;
- return abcd0 + abcd1 + abcd2 + abcd3 + i;
- })
- var i = 0;
- // Asignment inside function parameters
- bench(`destruct object func`, iters, function (i){
- (function({ abcd0, abcd1, abcd2, abcd3 }){
- return abcd0 + abcd1 + abcd2 + abcd3 + i;
- })(obj);
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement