Re: [nodejs] es6 'let' vs 'var' memory usage

139 views
Skip to first unread message

Aria Stewart

unread,
Dec 6, 2015, 9:30:56 AM12/6/15
to nod...@googlegroups.com

> On Dec 6, 2015, at 8:57 AM, jeongsik woo <realja...@gmail.com> wrote:
>
> Hi. I installed node.js 4.2.1(argon).
> I noticed new key 'let' of ES6. (https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let)
>
> I understood that let use lower memory than 'var'. because let has block scope, but 'var' has function scope.
>
> So I tested memory usage of var and let.
>
> But test result refer to 'var' uses lower memory than 'let'.
> differences of rss, heapTotal, heapUsed value of 'let' is bigger than 'var'.
>
> do I misunderstand 'let'?
>
> Sorry for my english writting, I am english newbie.
>
> below is my test code.

So there's enough complexity in V8 that a microbenchmark like yours may not show off real world scenarios. There are some differences: with `let`, you're making a new variable every time; with `var` you're re-using that. That means that there may be cases that the optimizer could convert back (where the effect won't leak), but those probably have to be written. I don't know the state of that in the optimizer, or if that's even an interesting optimization.

And then there's dead code elimination and all kinds of other optimizer tricks.

Then you've got a tight loop making garbage, and the garbage collector has to run periodically. So that will show a higher RSS, but it may not actually affect a real program unless your real program is at tight loop.

TL;DR: You're probably not measuring what you think you're measuring. if you are measuring that, what you're measuring is likely not relevant to results.

Aria

Angel Java Lopez

unread,
Dec 6, 2015, 9:30:56 AM12/6/15
to nod...@googlegroups.com
Hi! 

An English newbie, here, too ;-)

This is not a direct answer to your question, but I did a quick test (with only 10_000_000 cycles haha you have plenty of time ;-)

let.js: change the let to var in the for, and the time is near to var.js version
let.js: change the let to var in the f funcion, and the time is near to original let.js version

First conclusion: the let in the for has more cost than the let in f function, because it involves a local var to the for, while let in f function is totally equivalent to var

Angel "Java" Lopez
@ajlopez


On Sun, Dec 6, 2015 at 10:57 AM, jeongsik woo <realja...@gmail.com> wrote:
Hi. I installed node.js 4.2.1(argon).

I understood that let use lower memory than 'var'. because let has block scope, but 'var' has function scope.

So I tested memory usage of var and let.

But test result refer to 'var' uses lower memory than 'let'.
differences of rss, heapTotal, heapUsed value of 'let' is bigger than 'var'.

do I misunderstand 'let'? 

Sorry for my english writting, I am english newbie. 

below is my test code.

let.js
'use strict'
let pre = process.memoryUsage();

console.log('pre', pre);

function f() {
    let obj = {x: 12};
    return obj.x;
}

function test() {
  for (let i =0;i<10000000000;i++) {
    f();
  }
}
test();

let post = process.memoryUsage();
console.log('post', post);

var.js
'use strict';

var pre = process.memoryUsage();
console.log('pre', pre);
function f() {
  var obj = {x: 12};
  return obj.x;
}
function test() {
  for (var i =0;i<10000000000;i++) {
    f();
  }
}
test();

var post = process.memoryUsage();
console.log('post', post);


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2b96af14-79b1-4b92-8300-71c80426a021%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages