Are extraction functions performed?

I iterate through a large array (10 ^ 5 elements) and perform an operation for each.

for (var row in rows) { switch (operator) { case "op1": row += 1; break; case "op2": ... case "opN": break; } } 

For verification and readability, I would like to extract this internal switch statement into my own function, so the loop looks just like

 for (var row in rows) { this.updateRow(row, operator); } 

Will the overhead associated with calling the function 10 ^ 5 times cause a noticeable decrease in performance?

+4
source share
2 answers

Built-in functions will always be liiiitle bits faster than certain ones. This is because things like parameters and return do not need to be pushed and popped from the stack at runtime. This is usually not a big problem with newer machines, but with 10 ^ 5 functional challenges you can see a slight performance hit.

I would probably keep it in strict accordance. This is actually not very painful, and every little optimization helps!

+2
source

Yes

Using JSPerf, I profiled my example here: http://jsperf.com/inline-switch-vs-switch-function

I tested the inline switch statement with 4 simple arithmetic operators against the identical switch statement extracted into its own function over an array with 100k elements. I also tested it using a randomized switch operator, a best case operator (first switch option) and a worst case operator (last switch option).

The integrated switch operator exceeded the function across the board, outperforming the function by 150 op / s in the worst case, ~ 600 op / s in the best case.

In this situation, the inline switch statement will be noticeably faster.

+1
source

All Articles