Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MaxGraey/as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
https://github.com/MaxGraey/as-string-sink
assemblyscript buffer builder performace sink string webassembly
Last synced: 2 months ago
JSON representation
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
- Host: GitHub
- URL: https://github.com/MaxGraey/as-string-sink
- Owner: MaxGraey
- License: mit
- Created: 2021-05-04T13:10:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-17T00:14:12.000Z (almost 2 years ago)
- Last Synced: 2024-11-10T22:03:42.413Z (2 months ago)
- Topics: assemblyscript, buffer, builder, performace, sink, string, webassembly
- Language: TypeScript
- Homepage:
- Size: 231 KB
- Stars: 28
- Watchers: 5
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-assemblyscript - MaxGraey/as-string-sink - Efficient String Builder (Packages)
README
String Sink
===
[![Build Status](https://github.com/MaxGraey/as-string-sink/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/MaxGraey/as-string-sink/actions/workflows/test.yml?query=branch%3Amain)
[![npm](https://img.shields.io/npm/v/as-string-sink.svg?color=007acc&logo=npm)](https://www.npmjs.com/package/as-string-sink)An efficient dynamically sized string buffer (aka **String Builder**) for AssemblyScript.
## Interface
```ts
class StringSink {
static withCapacity(capacity: i32)constructor(initial: string = "", capacity: i32 = 32)
get length(): i32
get capacity(): i32// Append sting or substring
write(src: string, start?: i32, end?: i32): void
// Append sting or substring with new line
writeLn(src?: string, start?: i32, end?: i32): void
// Append single code point
writeCodePoint(code: i32): void
// Append any integer or floating point number
writeNumber(value: T): voidreserve(capacity: i32, clear?: bool): void
shrink(): void
clear(): void// Convert buffer to normal string
toString(): string
}
```## Benchmark Results
StringSink can be up to 4000 times faster than naive string concatenation! And up to 6 times faster than JS concat which uses rope data structure under the hood.
100 strings:
------------
```ts
String += JS: 0.013 ms
String += AS: 0.014 ms
StringSink AS: 0.0042 ms `(3x)`
```50,000 strings:
---------------
```ts
String += JS: 3.34 ms
String += AS: 509.54 ms
StringSink AS: 0.73 ms `(~700x)`
```200,000 strings:
----------------
```ts
String += JS: 11.93 ms
String += AS: 7622.72 ms
StringSink AS: 2.88 ms `(~2600x)`
```## Usage 1. String accumulation (+=)
non efficient example:
```ts
function toList(arr: string[]): string {
let res = "";
for (let i = 0, len = arr.length; i < len; i++) {
res += arr[i] + "\n";
}
return res;
}
```efficient with `StringSink`:
```ts
function toList(arr: string[]): string {
let res = new StringSink();
for (let i = 0, len = arr.length; i < len; i++) {
res.write(arr[i] + "\n");
}
return res.toString();
}
```even more efficient:
```ts
function toList(arr: string[]): string {
let res = new StringSink();
for (let i = 0, len = arr.length; i < len; i++) {
res.writeLn(arr[i]);
}
return res.toString();
}
```Complex example:
```ts
function zipAndStringify(names: string[], ages: i32[]): string {
assert(names.length == ages.length);let res = new StringSink();
res.writeLn('[');
for (let i = 0, len = names.length; i < len; i++) {
res.write(' { name: "');
res.write(names[i]);
res.write('", age: ');
res.writeNumber(ages[i]);
res.writeLn(' },');
}
res.write(']');
return res.toString();
}assert(zipAndStringify(
["Alan", "Elon", "John D."],
[109, 50, 51]
) == `[
{ name: "Alan", age: 109 },
{ name: "Elon", age: 50 },
{ name: "John D.", age: 51 },
]`);
```## Usage 2. String accumulation (+=) only part of string
non efficient example:
```ts
function toListSliced(arr: string[]): string {
let res = "";
for (let i = 0, len = arr.length; i < len; i++) {
res += arr[i].substring(1, 3);
}
return res;
}
```more efficient with `StringSink`:
```ts
function toListSliced(arr: string[]): string {
let res = new StringSink();
for (let i = 0, len = arr.length; i < len; i++) {
res.write(arr[i], 1, 3);
}
return res.toString();
}
```