1

The length property of strings in JavaScript is the number of code units, for example:

console.log("a".length); // 3

I have implemented a function that returns the number of Unicode points:

function length(str) {
  let l = 0;
  for (let i = 0; i < str.length; i++) {
    const code = str.charCodeAt(i);
    if (code >= 0xD800 && code <= 0xDBFF)
      i++;
    l += 1;
  }
  return l;
}

console.log(length("a")); // 2

However, if the string is very long, my implementation is not very effective. Does anyone have a better implementation? Why does not the ECMAScript provide a built-in method doing this job?

  • 3
    sure, `[..."a"].length` – georg May 14 '19 at 08:38
  • Here is the complete article : https://blog.jonnew.com/posts/poo-dot-length-equals-two – Sourabh Somani May 14 '19 at 08:43
  • `Array.from(str).length` – Sourabh Somani May 14 '19 at 08:44
  • @SourabhSomani What's the time complexity of this method? – Alexander Huang May 14 '19 at 08:50
  • @AlexanderDHuang `O(n)`, as every other way that iterates the string – Bergi May 14 '19 at 08:53
  • Maybe https://coolaj86.com/articles/how-to-count-unicode-characters-in-javascript/ offers a solution for you – KooiInc May 14 '19 at 08:58
  • @Bergi If the time complexity of `Array.from(str).length` is `O(n)`, it doesn't solve my problem because of my implementation of `length` is also `O(n)`. Is there an `O(1)` method? – Alexander Huang May 14 '19 at 09:03
  • @AlexanderDHuang No, there is no `O(1)` method, you always have to look at all characters to see how many code points they form. You would need a string (wrapper) implementation that always carries the count around with it. Why do you need an `O(1)` solution? – Bergi May 14 '19 at 09:16
  • @Bergi Because that calculating the length or the number of code points of strings is a frequent operation, of course, I want the time is `O(1)`. For example, [the `string.length` in JS is `O(1)`](https://stackoverflow.com/questions/30926007/is-the-javascript-string-length-constant-time), and `len()` in Python is also `O(1)` when applied to some built-in containers, e.g. `list`, `str`, and `bytearray`. – Alexander Huang May 14 '19 at 09:55
  • Accessing the `.length` is a constant-time operation, counting code points is not. Why do you need it that frequently? – Bergi May 14 '19 at 14:28

0 Answers0