Skip to content

perf: use String.fromCharCode.apply in floats32ToBase64 example#215

Open
rdgordon-index wants to merge 1 commit into
InteractiveAdvertisingBureau:mainfrom
rdgordon-index:update-vector-encoding-examples
Open

perf: use String.fromCharCode.apply in floats32ToBase64 example#215
rdgordon-index wants to merge 1 commit into
InteractiveAdvertisingBureau:mainfrom
rdgordon-index:update-vector-encoding-examples

Conversation

@rdgordon-index

Copy link
Copy Markdown
Contributor

Description:
The current encode example accumulates a binary string by appending one character per byte in a for...of loop. Each iteration allocates a new string, making the total cost O(n) allocations for an n-byte vector.

String.fromCharCode.apply(null, bytes) processes all bytes in a single native call, avoiding the per-byte re-allocations. Benchmarked with Benchmark.js against normalized unit-sphere float32 vectors across realistic embedding dimensions:

Dimension for…of (ops/sec) apply (ops/sec) speedup
spec example (10 floats, 40 B) 887,207 1,473,359 ×1.7
128-dim (512 B) 124,262 255,052 ×2.1
256-dim (1 KB) 48,772 132,912 ×2.7
768-dim / BERT-base (3 KB) 22,171 45,845 ×2.1
1536-dim / OpenAI ada (6 KB) 10,725 24,995 ×2.3
3072-dim / OpenAI large (12 KB) 5,167 10,920 ×2.1

The spec example row was reproduced with:

const Benchmark = require('benchmark');
const floats = [1.2345678, -2.5, 0.0, 3.1415927, 12345.678, -0.00012345, 42.42, -999.999, 0.000001, 987654.25];
const buf = new ArrayBuffer(floats.length * 4);
const view = new DataView(buf);
floats.forEach((x, i) => view.setFloat32(i * 4, x, true));
const bytes = new Uint8Array(buf);
new Benchmark.Suite()
  .add('for…of', () => { let s=''; for (const b of bytes) s += String.fromCharCode(b); btoa(s); })
  .add('apply',  () => btoa(String.fromCharCode.apply(null, bytes)))
  .on('cycle', e => console.log(String(e.target)))
  .run({ async: false });

Works in all modern browsers with no caveats. A comment in the code notes that very large inputs should be chunked into ~8192-byte blocks to stay within engine argument count limits, though this is not a concern for any embedding dimension currently in use.

The decode example is unchanged — there is no browser-compatible equivalent speedup for that path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant