Skip to content

Commit

Permalink
faster alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Jan 9, 2023
1 parent 4f39604 commit da7a164
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions benchmark/bench1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import protobuf from "protobufjs";

// Run with "node --loader ts-node/esm ./benchmark/bench1.ts"
/*
* json x 2,910,221 ops/sec ±0.46% (98 runs sampled)
* borsh x 9,893,398 ops/sec ±0.60% (245 runs sampled)
* protobujs x 9,513,112 ops/sec ±0.13% (244 runs sampled)
* json x 3,103,049 ops/sec ±0.53% (98 runs sampled)
* borsh x 11,503,732 ops/sec ±0.24% (244 runs sampled)
* protobujs x 9,732,443 ops/sec ±0.48% (247 runs sampled)
*/

function getRandomInt(max: number) {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/bench2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import protobuf from "protobufjs";
// Run with "node --loader ts-node/esm ./benchmark/bench2.ts"

/***
* json x 1,997,857 ops/sec ±0.33% (243 runs sampled)
* borsh x 3,570,224 ops/sec ±0.58% (242 runs sampled)
* protobujs x 3,357,032 ops/sec ±0.54% (241 runs sampled)
* json x 2,055,952 ops/sec ±0.33% (241 runs sampled)
* borsh x 3,973,669 ops/sec ±0.56% (243 runs sampled)
* protobujs x 3,365,144 ops/sec ±0.45% (243 runs sampled)
*/
function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/bench3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import crypto from 'crypto';


/*
* borsh x 2,197,862 ops/sec ±0.13% (246 runs sampled)
* protobujs x 2,105,680 ops/sec ±0.43% (246 runs sampled)
* borsh x 2,442,475 ops/sec ±0.44% (243 runs sampled)
* protobujs x 2,229,004 ops/sec ±0.50% (243 runs sampled)
*/

function getRandomInt(max: number) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dao-xyz/borsh",
"version": "5.0.0",
"version": "5.0.1",
"readme": "README.md",
"homepage": "https://github.com/dao-xyz/borsh-ts#README",
"description": "Binary Object Representation Serializer for Hashing simplified with decorators",
Expand Down
7 changes: 4 additions & 3 deletions src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { BorshError } from "./error.js";
import utf8 from '@protobufjs/utf8';
import { PrimitiveType, SmallIntegerType } from './types.js';

const allocUnsafe = (len: number): Uint8Array => { // TODO return fn instead for v8 fn optimization
const allocUnsafeFn = (): (len: number) => Uint8Array => { // TODO return fn instead for v8 fn optimization
if ((globalThis as any).Buffer) {
return (globalThis as any).Buffer.allocUnsafe(len)
return (globalThis as any).Buffer.allocUnsafe
}
return new Uint8Array(len)
return (len) => new Uint8Array(len);
}
const allocUnsafe = allocUnsafeFn();


export class BinaryWriter {
Expand Down

0 comments on commit da7a164

Please sign in to comment.