Is there a reason JavaScript developers don't use Array.push()?

前端 未结 6 1183
庸人自扰
庸人自扰 2020-12-01 05:29

I commonly see developers use an expression like the following in JavaScript:

arr = []
arr[arr.length] = \"Something\"
arr[arr.length] = \"Another thing\"
         


        
6条回答
  •  伪装坚强ぢ
    2020-12-01 05:45

    I believe that it's mostly habit.

    Some developers use it simply because it's the way they are used to do it, and haven't considered that push would be an alternative.

    Some developers have learned once upon a time that one method is much faster than another, and haven't reviewed this in light of the recent performance improvements of the Javascript engines.

    Personally I use push frequently. Most of the time readability and maintainability is more important than performance, at least when the performance impact is small enough. The performance tests posted in the answers here show that the performance difference between various methods isn't very big.

提交回复
热议问题