javascript : blocked scope inside loop , performance difference












1















I've a following code snippets ,



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
let obj;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj= data[i];
arr.push(obj.features);
}
}
}


I've declared variable obj outside loop , and in the following code snippets i'm going to declare obj inside the loop , like this



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
let obj= data[i];
arr.push(obj.features);
}
}
}


the memory allocation for variable obj will be released after the end of the loop , so i want to know which is best in terms of memory allocation and performance if the collection is big










share|improve this question


















  • 3





    Test it at jsperf and why not simple let arr = data.map(x => x.features)

    – Satpal
    Nov 22 '18 at 8:23













  • The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

    – Bergi
    Nov 22 '18 at 10:57











  • yeah ,thanks much!

    – AhammadaliPK
    Nov 22 '18 at 13:11
















1















I've a following code snippets ,



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
let obj;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj= data[i];
arr.push(obj.features);
}
}
}


I've declared variable obj outside loop , and in the following code snippets i'm going to declare obj inside the loop , like this



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
let obj= data[i];
arr.push(obj.features);
}
}
}


the memory allocation for variable obj will be released after the end of the loop , so i want to know which is best in terms of memory allocation and performance if the collection is big










share|improve this question


















  • 3





    Test it at jsperf and why not simple let arr = data.map(x => x.features)

    – Satpal
    Nov 22 '18 at 8:23













  • The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

    – Bergi
    Nov 22 '18 at 10:57











  • yeah ,thanks much!

    – AhammadaliPK
    Nov 22 '18 at 13:11














1












1








1


1






I've a following code snippets ,



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
let obj;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj= data[i];
arr.push(obj.features);
}
}
}


I've declared variable obj outside loop , and in the following code snippets i'm going to declare obj inside the loop , like this



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
let obj= data[i];
arr.push(obj.features);
}
}
}


the memory allocation for variable obj will be released after the end of the loop , so i want to know which is best in terms of memory allocation and performance if the collection is big










share|improve this question














I've a following code snippets ,



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
let obj;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj= data[i];
arr.push(obj.features);
}
}
}


I've declared variable obj outside loop , and in the following code snippets i'm going to declare obj inside the loop , like this



poupulateData(data) {
//lets assume variable data is an array of objects with more 400 data
let arr = ;
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
let obj= data[i];
arr.push(obj.features);
}
}
}


the memory allocation for variable obj will be released after the end of the loop , so i want to know which is best in terms of memory allocation and performance if the collection is big







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 8:18









AhammadaliPKAhammadaliPK

1,10821125




1,10821125








  • 3





    Test it at jsperf and why not simple let arr = data.map(x => x.features)

    – Satpal
    Nov 22 '18 at 8:23













  • The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

    – Bergi
    Nov 22 '18 at 10:57











  • yeah ,thanks much!

    – AhammadaliPK
    Nov 22 '18 at 13:11














  • 3





    Test it at jsperf and why not simple let arr = data.map(x => x.features)

    – Satpal
    Nov 22 '18 at 8:23













  • The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

    – Bergi
    Nov 22 '18 at 10:57











  • yeah ,thanks much!

    – AhammadaliPK
    Nov 22 '18 at 13:11








3




3





Test it at jsperf and why not simple let arr = data.map(x => x.features)

– Satpal
Nov 22 '18 at 8:23







Test it at jsperf and why not simple let arr = data.map(x => x.features)

– Satpal
Nov 22 '18 at 8:23















The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

– Bergi
Nov 22 '18 at 10:57





The variable should be allocated on the stack (if you don't use closures inside the loop scope) anyway, so it would not make any difference. The performance difference will be absolutely negligible, you should focus on writing clean and correct code.

– Bergi
Nov 22 '18 at 10:57













yeah ,thanks much!

– AhammadaliPK
Nov 22 '18 at 13:11





yeah ,thanks much!

– AhammadaliPK
Nov 22 '18 at 13:11












2 Answers
2






active

oldest

votes


















1














Variable declared within a loop will be declared every cycle, thus allocating extra memory. Also the variable is not immediately cleaned after a cycle. You can tell the garbage collector to run after each cycle by setting obj = null which will decrease your memory footprint, but will cost you some CPU time. Also keep in mind that you don't have direct control over the garbage collector. A JavaScript engine optimizes your code before running it and if there is enough free memory on your system it will most likely free memory when the CPU is not busy.



In general declaring variables outside a loop and cleaning them after is better for performance. Here is an example:



poupulateData(data) {
let arr = new Array(data.length); // set the size to save some memory
let obj = {}; //set it to object to avoid casting it when the loop starts
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj = data[i];
arr[i] = obj.features;
}
}
obj = null //clean some memory
i = null
}


Usually you don't need to care about this stuff in JavaScript. A pretty decent optimization is done before your code is being executed.






share|improve this answer



















  • 2





    "You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

    – Bergi
    Nov 22 '18 at 10:54











  • @Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

    – Nedko Dimitrov
    Nov 22 '18 at 11:02



















1














If you are after performance, in general when dealing with arrays it is advised to use the array methods provided, because they are optimised to work with arrays.






share|improve this answer


























  • The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

    – Bergi
    Nov 22 '18 at 10:58











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426539%2fjavascript-blocked-scope-inside-loop-performance-difference%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Variable declared within a loop will be declared every cycle, thus allocating extra memory. Also the variable is not immediately cleaned after a cycle. You can tell the garbage collector to run after each cycle by setting obj = null which will decrease your memory footprint, but will cost you some CPU time. Also keep in mind that you don't have direct control over the garbage collector. A JavaScript engine optimizes your code before running it and if there is enough free memory on your system it will most likely free memory when the CPU is not busy.



In general declaring variables outside a loop and cleaning them after is better for performance. Here is an example:



poupulateData(data) {
let arr = new Array(data.length); // set the size to save some memory
let obj = {}; //set it to object to avoid casting it when the loop starts
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj = data[i];
arr[i] = obj.features;
}
}
obj = null //clean some memory
i = null
}


Usually you don't need to care about this stuff in JavaScript. A pretty decent optimization is done before your code is being executed.






share|improve this answer



















  • 2





    "You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

    – Bergi
    Nov 22 '18 at 10:54











  • @Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

    – Nedko Dimitrov
    Nov 22 '18 at 11:02
















1














Variable declared within a loop will be declared every cycle, thus allocating extra memory. Also the variable is not immediately cleaned after a cycle. You can tell the garbage collector to run after each cycle by setting obj = null which will decrease your memory footprint, but will cost you some CPU time. Also keep in mind that you don't have direct control over the garbage collector. A JavaScript engine optimizes your code before running it and if there is enough free memory on your system it will most likely free memory when the CPU is not busy.



In general declaring variables outside a loop and cleaning them after is better for performance. Here is an example:



poupulateData(data) {
let arr = new Array(data.length); // set the size to save some memory
let obj = {}; //set it to object to avoid casting it when the loop starts
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj = data[i];
arr[i] = obj.features;
}
}
obj = null //clean some memory
i = null
}


Usually you don't need to care about this stuff in JavaScript. A pretty decent optimization is done before your code is being executed.






share|improve this answer



















  • 2





    "You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

    – Bergi
    Nov 22 '18 at 10:54











  • @Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

    – Nedko Dimitrov
    Nov 22 '18 at 11:02














1












1








1







Variable declared within a loop will be declared every cycle, thus allocating extra memory. Also the variable is not immediately cleaned after a cycle. You can tell the garbage collector to run after each cycle by setting obj = null which will decrease your memory footprint, but will cost you some CPU time. Also keep in mind that you don't have direct control over the garbage collector. A JavaScript engine optimizes your code before running it and if there is enough free memory on your system it will most likely free memory when the CPU is not busy.



In general declaring variables outside a loop and cleaning them after is better for performance. Here is an example:



poupulateData(data) {
let arr = new Array(data.length); // set the size to save some memory
let obj = {}; //set it to object to avoid casting it when the loop starts
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj = data[i];
arr[i] = obj.features;
}
}
obj = null //clean some memory
i = null
}


Usually you don't need to care about this stuff in JavaScript. A pretty decent optimization is done before your code is being executed.






share|improve this answer













Variable declared within a loop will be declared every cycle, thus allocating extra memory. Also the variable is not immediately cleaned after a cycle. You can tell the garbage collector to run after each cycle by setting obj = null which will decrease your memory footprint, but will cost you some CPU time. Also keep in mind that you don't have direct control over the garbage collector. A JavaScript engine optimizes your code before running it and if there is enough free memory on your system it will most likely free memory when the CPU is not busy.



In general declaring variables outside a loop and cleaning them after is better for performance. Here is an example:



poupulateData(data) {
let arr = new Array(data.length); // set the size to save some memory
let obj = {}; //set it to object to avoid casting it when the loop starts
if (data && data.length) {
for (let i = 0; i < data.length; i++) {
obj = data[i];
arr[i] = obj.features;
}
}
obj = null //clean some memory
i = null
}


Usually you don't need to care about this stuff in JavaScript. A pretty decent optimization is done before your code is being executed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 9:33









Nedko DimitrovNedko Dimitrov

523615




523615








  • 2





    "You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

    – Bergi
    Nov 22 '18 at 10:54











  • @Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

    – Nedko Dimitrov
    Nov 22 '18 at 11:02














  • 2





    "You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

    – Bergi
    Nov 22 '18 at 10:54











  • @Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

    – Nedko Dimitrov
    Nov 22 '18 at 11:02








2




2





"You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

– Bergi
Nov 22 '18 at 10:54





"You can tell the garbage collector to run" - no, clearing a variable does not automatically run the gargabe collector. Sure, it does mark the value as eligible for collection (assuming it's not referenced from elsewhere), but the GC decides itself when it will actually start collecting.

– Bergi
Nov 22 '18 at 10:54













@Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

– Nedko Dimitrov
Nov 22 '18 at 11:02





@Bergi probably I did not express myself correctly I tried to point exactly that. I wrote "... tell the GC..." not "will force it to do it". And the JS engine optimization may not respect your request . If you want help me to edit the answer to make it more clear.

– Nedko Dimitrov
Nov 22 '18 at 11:02













1














If you are after performance, in general when dealing with arrays it is advised to use the array methods provided, because they are optimised to work with arrays.






share|improve this answer


























  • The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

    – Bergi
    Nov 22 '18 at 10:58
















1














If you are after performance, in general when dealing with arrays it is advised to use the array methods provided, because they are optimised to work with arrays.






share|improve this answer


























  • The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

    – Bergi
    Nov 22 '18 at 10:58














1












1








1







If you are after performance, in general when dealing with arrays it is advised to use the array methods provided, because they are optimised to work with arrays.






share|improve this answer















If you are after performance, in general when dealing with arrays it is advised to use the array methods provided, because they are optimised to work with arrays.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 13:42

























answered Nov 22 '18 at 9:19









squeekyDavesqueekyDave

415216




415216













  • The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

    – Bergi
    Nov 22 '18 at 10:58



















  • The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

    – Bergi
    Nov 22 '18 at 10:58

















The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

– Bergi
Nov 22 '18 at 10:58





The array methods are just as well optimised as explicit looping, there's no difference in performance. (But sure, using map here would be cleaner)

– Bergi
Nov 22 '18 at 10:58


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426539%2fjavascript-blocked-scope-inside-loop-performance-difference%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory