New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

as-string-sink

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

as-string-sink

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

  • 0.4.2
  • npm
  • Socket score

Version published
Weekly downloads
15
decreased by-70.59%
Maintainers
1
Weekly downloads
 
Created
Source

String Sink

Build Status 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

  write(src: string, start?: i32, end?: i32): void
  writeLn(src: string, start?: i32, end?: i32): void
  writeCodePoint(code: i32): void

  reserve(capacity: i32, clear?: bool): void
  shrink(): void
  clear(): void

  toString(): string
}

Benchmark Results

StringSink can be up to 8700 times faster than native string concatenation!

100 strings:
------------
String += JS:  0.013 ms
String += AS:  0.017 ms
StringSink AS: 0.0034 ms

50,000 strings:
---------------
String += JS:  3.89 ms
String += AS:  1219.44 ms
StringSink AS: 0.54 ms

200,000 strings:
----------------
String += JS:  11.29 ms
String += AS:  18360.15 ms
StringSink AS: 2.28 ms

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();
}

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();
}

Keywords

FAQs

Package last updated on 06 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc