Is this good use of javascript or can I better use variables

79 views
Skip to first unread message

Roelof Wobben

unread,
Dec 27, 2014, 1:39:15 PM12/27/14
to nod...@googlegroups.com
Hello,

I do the nodeschool.io course and on the javascripting part I have to put the length of a string to the screen.

So I did this :

/**
* Created with Ecomm - node.
* User: roelof1967
* Date: 2014-12-27
* Time: 06:34 PM
* To change this template use Tools | Templates.
*/
var example = "example string"
console.log(example.length);

Is this valid and good javascript or can I better use a variable for the length and use that on console.log  ?

Roelof

Thomas GMail

unread,
Dec 27, 2014, 2:26:00 PM12/27/14
to nod...@googlegroups.com
Well, this is sort of a toy example. The answer in the real world would depend on whether you will need to reuse the length throughout your module or if this is a one and done for logging purposes. 

Sent from my iPhone
--
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/19e45849-6ce4-4634-9359-415771f8ce43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roelof Wobben

unread,
Dec 27, 2014, 2:34:33 PM12/27/14
to nod...@googlegroups.com
Oke

Let's say it a one and one logging purpose.

Roelof


Op zaterdag 27 december 2014 20:26:00 UTC+1 schreef Thomas Murphy:

Ryan Schmidt

unread,
Dec 27, 2014, 11:21:03 PM12/27/14
to nod...@googlegroups.com

> On Dec 27, 2014, at 1:34 PM, Roelof Wobben wrote:
>
> Oke
>
> Let's say it a one and one logging purpose.

I don't understand.


>> var example = "example string"
>> console.log(example.length);
>>
>> Is this valid and good javascript

This is fine.


>> or can I better use a variable for the length and use that on console.log ?

If your JavaScript will run in an old web browser with a slow and/or poorly-optimized JavaScript engine (old versions of Internet Explorer, especially), and you will be using the length of the string repeatedly (in a loop, for example), then you can get slightly better performance by using an intermediate variable:

var example = "example string";
var exampleLength = example.length;
for (var i = 0; i < 10; ++i) {
console.log(exampleLength);
}

But don't worry about those kinds of micro-optimizations when first learning JavaScript, and don't worry about them at all for modern JavaScript engines like the v8 engine found in Google Chrome and in nodejs.

Using intermediate variables for properties you use in several places in code can also reduce the size of your code when minified, but that's another micro-optimization, and only applicable if the code will be sent over the wire to a web browser, and not applicable for server-side code running under nodejs for example.

Roelof Wobben

unread,
Dec 28, 2014, 4:49:20 AM12/28/14
to nod...@googlegroups.com
Oke, Case can be closed.

Roelof


Op zondag 28 december 2014 05:21:03 UTC+1 schreef ryandesign:
Reply all
Reply to author
Forward
0 new messages