Re: [whatwg/encoding] Fast byteLength() (Issue #333)

I am not 100% this is correct ... *but* ... it's also pretty slow and I start wondering if the slowness doesn't come directly from string internal code-points:

```js
"use strict"
module.exports = (input) => {
  let total = 0;
  for (const c of input) {
    const p = c.codePointAt(0);
    if (p < 0x80) total += 1;
    else if (p < 0x800) total += 2;
    else total += (p & 0xD800) ? 4 : 3;
  }
  return total;
};
```

Results on my laptop:

```
./benchmarks/blob.js:           405’174.6 ops/sec (±5’563.9, p=0.001, o=6/100) severe outliers=2
./benchmarks/buffer.js:         45’447’421.7 ops/sec (±659’453.6, p=0.001, o=0/100)
./benchmarks/codepoint.js:      15’096’778.8 ops/sec (±185’463.1, p=0.001, o=0/100)
./benchmarks/implementation.js: 65’565’103.6 ops/sec (±1’127’578.0, p=0.001, o=4/100) severe outliers=2
./benchmarks/textencoder.js:    2’698’465.4 ops/sec (±97’198.0, p=0.001, o=0/100)
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/encoding/issues/333#issuecomment-2252167467
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/encoding/issues/333/2252167467@github.com>

Received on Friday, 26 July 2024 07:46:44 UTC