devicescript icon indicating copy to clipboard operation
devicescript copied to clipboard

string concatenation is qudratic

Open mmoskal opened this issue 3 years ago • 1 comments

This should run reasonably fast, but doesn't.

let s = ""
for (let i = 0; i < 2000; ++i) {
  s += "x"
}
ds.assert(s.length == 2000 && s[100] == "x")

mmoskal avatar Dec 13 '22 13:12 mmoskal

Note that Array.join is implemented, so the recommended way of doing the above is:

let sa: string[] = []
for (let i = 0; i < 2000; ++i) {
  sa.push("x")
}
const s = sa.join("")
ds.assert(s.length == 2000 && s[100] == "x")

mmoskal avatar Apr 24 '23 18:04 mmoskal