Javascript Concatenate Array to String

前端 未结 2 1431
臣服心动
臣服心动 2021-01-11 13:51

I am using D3.js and often find myself dynamically building transform attributes (or d attributes on path elements). Both of these of

2条回答
  •  再見小時候
    2021-01-11 14:03

    When JavaScript coerces an array to a string, it actually call: .join(',') on the array. So you're actually going to be getting better performance with .join(',') manually as opposed to leaving it up to the interpreter to notice you're coercing the array. So: x + ',' + y is the fastest, [x, y].join(',') is the best practice(since it makes it easier to modify the behavior), and [x, y] is a tiny bit slower than manually calling .join and can be unreadable at times, but it's more convenient.

提交回复
热议问题