In a nutshell, multiplying an array by a number is usually faster than copying it. This is because multiplication is a simpler operation for CPUs, while copying involves looping and assigning values, requiring more steps. Several factors contribute to multiplication's potential speed advantage:
- Multiplication: Accesses each element, performs a simple arithmetic operation, and stores the result. This process benefits from CPU caches, as both the read (original value) and write (multiplied value) operations are localized and predictable.
- Copying: Although similar in accessing each element, copying often involves additional memory allocations and potential cache misses when duplicating the array structure.
- Multiplication: The multiplication operation (* 2) is computationally straightforward and highly optimized by modern processors.
- Copying: Depending on the method (e.g., for-loop, slice, spread operator), copying may involve more complex operations under the hood, including array resizing and managing memory pointers, which can introduce overhead.
- Multiplication: Involves working with a single array and storing results in a new array of the same size. The memory management is straightforward.
- Copying: Some copying methods may create intermediate arrays or involve higher-level abstractions (like Array.from or spread syntax), which can temporarily increase memory usage and invoke garbage collection more frequently, affecting performance.
JavaScript engines like V8 (used in Chrome and Node.js) are highly optimized for arithmetic operations. Simple loops with arithmetic are often optimized better than loops that manage array structures.
Multiplication operations tend to be more cache-efficient because they involve straightforward arithmetic operations that the CPU can handle more effectively. Copying, on the other hand, may involve more memory accesses and management, leading to less efficient use of the CPU cache.
Overall, the specific nature of array copying especially when using higher-level methods tends to introduce more complexity and overhead compared to the straightforward arithmetic involved in multiplying array elements. This may lead to faster multiplication in observed tests.