All Projects → MaxGraey → as-string-sink

MaxGraey / as-string-sink

Licence: MIT license
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to as-string-sink

obj-to-table
Create a table from an array of objects
Stars: ✭ 15 (-34.78%)
Mutual labels:  string
website
AssemblyScript's website and documentation.
Stars: ✭ 47 (+104.35%)
Mutual labels:  assemblyscript
sphinx-jekyll-builder
sphinx builder that outputs jekyll compatible markdown files with frontmatter
Stars: ✭ 18 (-21.74%)
Mutual labels:  builder
ngx-aws-deploy
☁️🚀 Deploy your Angular app to Amazon S3 directly from the Angular CLI 🚀☁️
Stars: ✭ 84 (+265.22%)
Mutual labels:  builder
SilentETHMiner
A Silent (Hidden) Ethereum (ETH & ETC) Miner Builder
Stars: ✭ 219 (+852.17%)
Mutual labels:  builder
dauntless-builder
Create and share Dauntless builds with your friends!
Stars: ✭ 50 (+117.39%)
Mutual labels:  builder
Competitive Programming
Programming👨‍💻 Questions on BinarySearch💻, LeetCode💻, CodeChef💻, Codeforces💻,DSA 450
Stars: ✭ 188 (+717.39%)
Mutual labels:  string
Nginx-builder
A tool to build deb or rpm package of required Nginx version from the source code, with the ability to connect third-party modules. Nginx parameters are set in the yaml configuration file.
Stars: ✭ 143 (+521.74%)
Mutual labels:  builder
comment-mark
Interpolate strings with HTML comment markers!
Stars: ✭ 21 (-8.7%)
Mutual labels:  string
msbotframework-mongo-middlelayer
Microsoft Bot framework: Using MongoDB as storage for conversational states, data and context
Stars: ✭ 36 (+56.52%)
Mutual labels:  builder
String.prototype.trim
ES5 spec-compliant shim for String.prototype.trim
Stars: ✭ 13 (-43.48%)
Mutual labels:  string
now-custom-runtime
ZEIT Now v2.0 builder for custom AWS Lambda runtimes
Stars: ✭ 21 (-8.7%)
Mutual labels:  builder
dbs
SQL Builder 工具,给爱拼 SQL 的你。
Stars: ✭ 47 (+104.35%)
Mutual labels:  builder
zlib
Pure javascript implementation of Zlib nodejs core module.The zlib module provides compression functionality implemented using Gzip and Deflate/Inflate.
Stars: ✭ 14 (-39.13%)
Mutual labels:  buffer
makestudio
Setup your Delphi Environment very easily - build your projects with more comfort
Stars: ✭ 43 (+86.96%)
Mutual labels:  builder
node-pg-large-object
Large object support for PostgreSQL clients using the node-postgres library.
Stars: ✭ 31 (+34.78%)
Mutual labels:  buffer
Uix-Page-Builder
Uix Page Builder is a design system that it is simple content creation interface.
Stars: ✭ 20 (-13.04%)
Mutual labels:  builder
to-ico
Convert PNG to ICO in memory
Stars: ✭ 115 (+400%)
Mutual labels:  buffer
jest-serializer-html-string
A better Jest snapshot serializer for plain html strings
Stars: ✭ 17 (-26.09%)
Mutual labels:  string
DPB
Dynamic Project Builder
Stars: ✭ 22 (-4.35%)
Mutual labels:  builder

String Sink

npm

An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript.

Interface

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<T>(value: T): void

  reserve(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:

String += JS:  0.019 ms
String += AS:  0.016 ms
StringSink AS: 0.0043 ms `(4x)`

50,000 strings:

String += JS:  3.70 ms
String += AS:  526.16 ms
StringSink AS: 0.48 ms `(1096x)`

200,000 strings:

String += JS:  11.95 ms
String += AS:  8236.82 ms
StringSink AS: 2.01 ms `(4097x)`

Usage 1. String accumulation (+=)

non efficient example:

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:

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:

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:

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:

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:

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();
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].