Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Arrray fill vs looping thru.

13 views
Skip to first unread message

jonas.t...@gmail.com

unread,
Nov 4, 2017, 7:11:37 AM11/4/17
to
Could i use array fill instead of loop thru filling up values but how would i set offset?

offset=0;
for (var i=0;i<numberofvalues){
dval.Array(countval[i]).fill(i);
offset=offset+countval[i];
}

But i do not understand can i set an offset with fill, so as you see i never use it above.

***HOW DO I MAKE THE FILL AT THE START ENTRY USING OFFSET***?
Is the fill function faster then using loop thru, i save a loop?

jonas.t...@gmail.com

unread,
Nov 4, 2017, 7:20:47 AM11/4/17
to
This is the original code i try to replace, the index of countval represent "a stored value" then actual element at each index "is number of occurences of stored value", can i remove the innerloop below using fill?
And will it be faster?

I would be glad on some help howto use fill with an offset.
I do understand that Array(3).fill(4) will fill element 1-3 in whatever array with 4,4,4 but that is index 0,1,2? how can i set an offset "startpoint".

JT



for (var j=start;j<end;j++){
temp=countval[j]
for (var k=0;k<temp;k++){
dval[p]=j;
p++;
}
}

jonas.t...@gmail.com

unread,
Nov 4, 2017, 7:29:50 AM11/4/17
to
But how does fill work with statement like Array(0).fill(4) will it still make the function call that would be a waste so i hope not?

JJ

unread,
Nov 4, 2017, 7:53:13 AM11/4/17
to
On Sat, 4 Nov 2017 04:29:44 -0700 (PDT), jonas.t...@gmail.com wrote:

> Den lördag 4 november 2017 kl. 12:20:47 UTC+1 skrev jonas.t...@gmail.com:
>> Den lördag 4 november 2017 kl. 12:11:37 UTC+1 skrev jonas.t...@gmail..com:
>>> Could i use array fill instead of loop thru filling up values but how would i set offset?
>>>
>>> offset=0;
>>> for (var i=0;i<numberofvalues){
>>> dval.Array(countval[i]).fill(i);
>>> offset=offset+countval[i];
>>> }
>>>
>>> But i do not understand can i set an offset with fill, so as you see i never use it above.
>>>
>>> ***HOW DO I MAKE THE FILL AT THE START ENTRY USING OFFSET***?
>>> Is the fill function faster then using loop thru, i save a loop?
>>
>> This is the original code i try to replace, the index of countval represent "a stored value" then actual element at each index "is number of occurences of stored value", can i remove the innerloop below using fill?
>> And will it be faster?
>>
>> I would be glad on some help howto use fill with an offset.
>> I do understand that Array(3).fill(4) will fill element 1-3 in whatever array with 4,4,4 but that is index 0,1,2? how can i set an offset "startpoint".
>>
>> JT
>>
>> for (var j=start;j<end;j++){
>> temp=countval[j]
>> for (var k=0;k<temp;k++){
>> dval[p]=j;
>> p++;
>> }
>> }
>
> But how does fill work with statement like Array(0).fill(4) will it still make the function call that would be a waste so i hope not?

Check the MDN reference for the `fill()` method.

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill>

You should bookmark that site too, since it's a pretty good reference for
most web specifications.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 8:07:08 AM11/4/17
to
I think i get it

Example fruits.fill("Kiwi", 2, 4)

offset=0;
for (var i=0;i<numberofvalues){
dval.Array(countval[i]).fill(i,offset,offset+countval[i]);
offset=offset+countval[i];
}

Does it seem reasonable?

jonas.t...@gmail.com

unread,
Nov 4, 2017, 8:24:52 AM11/4/17
to
Well this look better "correct"

offset=0;
for (var i=0;i<numberofvalues;i++){
dval.fill(i,offset,offset+countval[i]);
offset=offset+countval[i];
}
I think i got it to work, but the timing seem to be the same, but this is very few occurences on every entry 0-3, if the sample data more dense it could give positive effects?
0 new messages