6

Just wondering, what is the most efficient way to divide an array by a scalar? I can clearly loop over it, but efficiency is paramount in my case.

Common trivial way:

var array_2 = []
var array_1 = [original_data
var divisor = my_scalar
for(var i = 0, length = array.length; i < length; i++){
            array_2.push(array[i]/divisor)
         }

Any trick I can use (or a new approach altogether)?

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Asher11
  • 1,295
  • 2
  • 15
  • 31
  • 2
    `var array_2 = array_1.map(i => i / divisor);` – tymeJV Mar 03 '17 at 16:56
  • cool trying it right away – Asher11 Mar 03 '17 at 16:57
  • @tymeJV: What makes you think a bunch of function calls will be more efficient than a `for` loop? (The difference in efficiency is ***extraordinarily*** unlikely to matter, mind...) – T.J. Crowder Mar 03 '17 at 16:57
  • @T.J.Crowder - I didnt say more efficient - I mainly focused on the "new approach all together" part of the question. – tymeJV Mar 03 '17 at 16:58
  • 2
    Note that your common trivial way could be more efficient (depending on which JavaScript engine you're talking about): `array_2[i] = array[i] / divisor` (not `push`). – T.J. Crowder Mar 03 '17 at 16:58
  • @tymeJV: It's just both the title and text emphasize efficiency... – T.J. Crowder Mar 03 '17 at 16:59
  • Pre-allocate the `array_2` and then use @T.J.Crowder's solution. Some implementations are optimized for homogeneous arrays. `var array_2 = new Array(original.length)` –  Mar 03 '17 at 17:01
  • @squint: You have to be careful doing that, since arrays aren't really arrays. IIRC, some years back I checked, and some engines preferred what you did there, and others preferred that you didn't. – T.J. Crowder Mar 03 '17 at 17:02
  • @T.J.Crowder: Interesting. IIRC Chrome has optimized for this, but not sure about others. –  Mar 03 '17 at 17:03
  • 1
    This answer on [SIMD mathematics](http://stackoverflow.com/a/34693609/) describes a possibly more efficient method but it seems to not be widely supported. Other than that, all the solutions provided, whether using `map` or just a loop will be generally the same in efficiency. – Justin Hellreich Mar 03 '17 at 17:04
  • @JustinHellreich: Nice! I was wondering if there were some optimizations that would use SIMD but didn't realize there was an actual API defined for it. –  Mar 03 '17 at 17:07

3 Answers3

6

You've said you want the most efficient way. Fairly sure what you're doing is close, but you want assignment rather than push, and in at least some JavaScript engines (V8 for instance, in the Chromium browsers), if you tell the Array constructor an initial length, they'll pre-allocate backing storage (even though the array starts with no elements in it):

var array_2 = Array(array.length);
for(var i = 0, length = array.length; i < length; i++){
    array_2[i] = array[i] / divisor;
}

Having said that: The difference between that and map is going to be very very very very very very small and it's an exceptionally rare use case where it would matter. Whereas map is clear, simple, short...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

You could use map for that:

var newData = data.map(function(item) { return item/scalar } )
Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62
cyr_x
  • 13,987
  • 2
  • 32
  • 46
2
const numbers = [350,451,758,456,999];
const dividedNum = num => num/7;
const output1 = numbers.map(dividedNum);
console.log(output1)
General Grievance
  • 4,555
  • 31
  • 31
  • 45