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 - npm Package Compare versions

Comparing version 0.4.2 to 0.5.0

30

assembly/__tests__/general.spec.ts

@@ -118,2 +118,32 @@ import { StringSink } from "../index";

it("test writeNumber", () => {
let sink = new StringSink("");
let str = "";
str += "f64.eps: ";
sink.write(str);
str += (-F64.EPSILON).toString();
sink.writeNumber(-F64.EPSILON);
expect(sink.toString()).toBe(str);
// sink.writeNumber(<i8>-128);
str += ", i8.val: ";
sink.write(", i8.val: ");
str += (<i8>-127).toString();
sink.writeNumber(<i8>-127);
expect(sink.toString()).toBe(str);
str += ", u64.max: ";
sink.write(", u64.max: ");
str += u64.MAX_VALUE.toString();
sink.writeNumber(u64.MAX_VALUE);
expect(sink.toString()).toBe(str);
});
it("clear for less than 32 lenght capacity", () => {

@@ -120,0 +150,0 @@ let sink = new StringSink("hello");

47

assembly/index.ts

@@ -0,1 +1,3 @@

import { itoa_buffered, dtoa_buffered } from "util/number";
const MIN_BUFFER_LEN = 32;

@@ -36,3 +38,3 @@ const MIN_BUFFER_SIZE: u32 = MIN_BUFFER_LEN << 1;

get length(): i32 {
return <i32>(this.offset >> 1);
return this.offset >> 1;
}

@@ -70,5 +72,4 @@

writeLn(src: string, start: i32 = 0, end: i32 = i32.MAX_VALUE): void {
writeLn(src: string = "", start: i32 = 0, end: i32 = i32.MAX_VALUE): void {
let len = src.length as u32;
if (start != 0 || end != i32.MAX_VALUE) {

@@ -83,11 +84,7 @@ let from: i32;

if (!len) return;
let size = len << 1;
this.ensureCapacity(size + 2);
let offset = this.offset;
let dest = changetype<usize>(this.buffer) + offset;
memory.copy(dest, changetype<usize>(src) + (<usize>start << 1), size);
if (size) memory.copy(dest, changetype<usize>(src) + (<usize>start << 1), size);
store<u16>(dest + size, NEW_LINE_CHAR);

@@ -117,2 +114,31 @@ this.offset = offset + (size + 2);

writeNumber<T extends number>(value: T): void {
let offset = this.offset;
if (isInteger<T>()) {
let maxCapacity = 0;
// this also include size for sign
if (sizeof<T>() == 1) {
maxCapacity = 4 << 1;
} else if (sizeof<T>() == 2) {
maxCapacity = 6 << 1;
} else if (sizeof<T>() == 4) {
maxCapacity = 11 << 1;
} else if (sizeof<T>() == 8) {
maxCapacity = 21 << 1;
}
this.ensureCapacity(maxCapacity);
offset += itoa_buffered(
changetype<usize>(this.buffer) + offset,
value
) << 1;
} else {
this.ensureCapacity(32 << 1);
offset += dtoa_buffered(
changetype<usize>(this.buffer) + offset,
value
) << 1;
}
this.offset = offset;
}
reserve(capacity: i32, clear: bool = false): void {

@@ -145,6 +171,5 @@ if (clear) this.offset = 0;

@inline protected ensureCapacity(deltaSize: u32): void {
let oldSize = this.offset;
@inline protected ensureCapacity(deltaBytes: u32): void {
let buffer = this.buffer;
let newSize = oldSize + deltaSize;
let newSize = this.offset + deltaBytes;
if (newSize > <u32>buffer.byteLength) {

@@ -151,0 +176,0 @@ this.buffer = changetype<ArrayBuffer>(__renew(

{
"name": "as-string-sink",
"version": "0.4.2",
"version": "0.5.0",
"description": "An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript",

@@ -28,3 +28,3 @@ "keywords": [

"@as-pect/cli": "^6.2.4",
"assemblyscript": "^0.19.5"
"assemblyscript": "^0.19.20"
},

@@ -31,0 +31,0 @@ "files": [

@@ -19,5 +19,10 @@ String Sink

// Append sting or substring
write(src: string, start?: i32, end?: i32): void
writeLn(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

@@ -28,2 +33,3 @@ reserve(capacity: i32, clear?: bool): void

// Convert buffer to normal string
toString(): string

@@ -35,22 +41,26 @@ }

StringSink can be up to 8700 times faster than native string concatenation!
StringSink can be up to 8000 times faster than naive string concatenation! And up to 5 times faster than JS concat which uses rope data structure under the hood.
```json
100 strings:
------------
```ts
String += JS: 0.013 ms
String += AS: 0.017 ms
StringSink AS: 0.0034 ms
StringSink AS: 0.0034 ms `(5x)`
```
50,000 strings:
---------------
```ts
String += JS: 3.89 ms
String += AS: 1219.44 ms
StringSink AS: 0.54 ms
StringSink AS: 0.54 ms `(2260x)`
```
200,000 strings:
----------------
```ts
String += JS: 11.29 ms
String += AS: 18360.15 ms
StringSink AS: 2.28 ms
StringSink AS: 2.28 ms `(8300x)`
```

@@ -96,2 +106,31 @@

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

@@ -98,0 +137,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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