/    Sign up×
Community /Pin to ProfileBookmark

Which what to best re-initialize or clear an array?

In the following demo only script, I clear (reset) the “tarr” two different ways.
My questions are:
1. Is one way better than the other? More acceptable? Or are they both the same?
2. Is there a concern with “memory leakage” using either method?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />

<title> Clear Array Contents </title>

</head>
<body>
<button onclick=”initArr(arr1)”>Initial Array (1..5)</button>
<button onclick=”initArr(arr2)”>Initial Array (5..1)</button>
<button onclick=”initArr(arr1.concat(arr2))”>concat Arrays</button>

<p>
<!– <button onclick=”showArr(tarr)”>Show tarray</button> –>

<button onclick=”clearArr1(tarr)”>Clear []</button>
<button onclick=”clearArr2(tarr)”>Clear =0</button>
<p>
<div id=”debug”></div>

<script type=”text/javascript”>
var arr1 = [1,2,3,4,5];
var arr2 = [5,4,3,2,1];

var tarr = []; // global array

function initArr(arr) { tarr = arr.slice(0); showArr(tarr); }
function showArr(arr) { document.getElementById(‘debug’).innerHTML = arr; }

function clearArr1(arr) { tarr = []; showArr(tarr); }
function clearArr2(arr) { tarr.length = 0; showArr(tarr); }

</script>

</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TcobbNov 23.2013 — Greetings----

You pose an interesting question. I had no idea as to the answer, and I might not still. But I did conduct a speed test about 20 or so times, and it appears that assigning the variable to an empty array is significantly faster than setting the length property to zero. I ran the program using node,js which uses Chrome's javascript engine It may be however that you will get wildly different results with different js engines.

[CODE]function lenR(){
var arr, st, fin, i, d = new Date();
st = d.getTime();
for(i = 0; i < 10000000; i++){
arr = [1,2,3,4,5,6,7,8,9,0];
arr.length = 0;
}
d = new Date();
fin = d.getTime();
console.log("time for setting length method was " + (fin - st)+ " millisecs");
}

function lenE(){
var arr, st, fin, i, d = new Date();
st = d.getTime();
for(i = 0; i < 10000000; i++){
arr = [1,2,3,4,5,6,7,8,9,0];
arr = [];
}
d = new Date();
fin = d.getTime();
console.log("time for assigning to an empty array was " + (fin - st)+ " millisecs");
}

lenR();
lenE();[/CODE]
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...