uint8-util
Advanced tools
| import Benchmark from 'benchmark' | ||
| globalThis.Benchmark = Benchmark | ||
| const bin = 'bZDÑ\x98\x93Kw¤[©UU)[\\' | ||
| const hex = '625a44d198934b77a45ba95555295b5c' | ||
| const suite = new Benchmark.Suite() | ||
| const alph = '0123456789abcdef' | ||
| const bin2hex = str => { | ||
| let res = '' | ||
| let c | ||
| let i = 0 | ||
| const len = str.length | ||
| while (i < len) { | ||
| c = str.charCodeAt(i++) | ||
| res += alph[c >> 4] | ||
| res += alph[c & 0xF] | ||
| } | ||
| return res | ||
| } | ||
| suite | ||
| .add('Buffer.from', function () { | ||
| const res = Buffer.from(bin, 'binary').toString('hex') | ||
| if (res !== hex) { | ||
| throw new Error('String decoding failed') | ||
| } | ||
| }) | ||
| .add('wtcommon', function () { | ||
| const res = bin2hex(bin) | ||
| if (res !== hex) { | ||
| console.log(res, hex) | ||
| throw new Error('String decoding failed') | ||
| } | ||
| }) | ||
| suite | ||
| // add listeners | ||
| .on('cycle', (event) => { | ||
| console.log(String(event.target)) | ||
| }) | ||
| .on('complete', function () { | ||
| console.log('Fastest is ' + this.filter('fastest').map('name') + '\n') | ||
| }) | ||
| .on('error', (err) => { | ||
| console.error(err) | ||
| }) | ||
| // run async | ||
| .run({ async: true }) |
| import Benchmark from 'benchmark' | ||
| globalThis.Benchmark = Benchmark | ||
| const bin = 'bZDÑ\x98\x93Kw¤[©UU)[\\' | ||
| const hex = '625a44d198934b77a45ba95555295b5c' | ||
| const suite = new Benchmark.Suite() | ||
| const hex2binary = hex => { | ||
| let string = '' | ||
| for (let i = 0, l = hex.length; i < l; i += 2) { | ||
| string += String.fromCharCode(parseInt(hex.substr(i, 2), 16)) | ||
| } | ||
| return string | ||
| } | ||
| const hex2binspread = hex => { | ||
| const points = new Array(hex.length / 2) | ||
| for (let i = 0, l = hex.length / 2; i < l; ++i) { | ||
| points[i] = parseInt(hex.substr(i * 2, 2), 16) | ||
| } | ||
| return decodeCodePointsArray(points) | ||
| } | ||
| const MAX_ARGUMENTS_LENGTH = 0x10000 | ||
| function decodeCodePointsArray (codePoints) { | ||
| const len = codePoints.length | ||
| if (len <= MAX_ARGUMENTS_LENGTH) { | ||
| return String.fromCharCode(...codePoints) | ||
| } | ||
| let res = '' | ||
| let i = 0 | ||
| while (i < len) { | ||
| res += String.fromCharCode(...codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)) | ||
| } | ||
| return res | ||
| } | ||
| suite | ||
| .add('Buffer.from', function () { | ||
| const res = Buffer.from(hex, 'hex').toString('binary') | ||
| if (res !== bin) { | ||
| console.dir(res) | ||
| throw new Error('String decoding failed') | ||
| } | ||
| }) | ||
| .add('wtcommon', function () { | ||
| const res = hex2binary(hex) | ||
| if (res !== bin) { | ||
| throw new Error('String decoding failed') | ||
| } | ||
| }) | ||
| .add('wtcommonspread', function () { | ||
| const res = hex2binspread(hex) | ||
| if (res !== bin) { | ||
| throw new Error('String decoding failed') | ||
| } | ||
| }) | ||
| suite | ||
| // add listeners | ||
| .on('cycle', (event) => { | ||
| console.log(String(event.target)) | ||
| }) | ||
| .on('complete', function () { | ||
| console.log('Fastest is ' + this.filter('fastest').map('name') + '\n') | ||
| }) | ||
| .on('error', (err) => { | ||
| console.error(err) | ||
| }) | ||
| // run async | ||
| .run({ async: true }) |
+7
-9
@@ -9,14 +9,12 @@ import { createHash } from 'node:crypto' | ||
| export const text2arr = str => { | ||
| return new Uint8Array(Buffer.from(str, 'utf-8')) | ||
| } | ||
| export const text2arr = str => new Uint8Array(Buffer.from(str, 'utf-8')) | ||
| export const arr2base = buffer => { | ||
| return Buffer.from(buffer).toString('base64') | ||
| } | ||
| export const arr2base = buffer => Buffer.from(buffer).toString('base64') | ||
| export const base2arr = str => { | ||
| return new Uint8Array(Buffer.from(str, 'base64')) | ||
| } | ||
| export const base2arr = str => new Uint8Array(Buffer.from(str, 'base64')) | ||
| export const hex2bin = hex => Buffer.from(hex, 'hex').toString('binary') | ||
| export const bin2hex = bin => Buffer.from(bin, 'binary').toString('hex') | ||
| export const hash = async (data, format, algo = 'sha1') => { | ||
@@ -23,0 +21,0 @@ algo = algo.replace('sha-', 'sha') |
+34
-11
@@ -1,2 +0,2 @@ | ||
| import { arr2hex } from './util.js' | ||
| import { arr2hex, alphabet } from './util.js' | ||
| import { decode, encode } from 'base64-arraybuffer' | ||
@@ -6,18 +6,41 @@ | ||
| // 50% slower at < 48 chars, but little impact at 4M OPS/s vs 8M OPS/s | ||
| export const arr2text = (buffer) => { | ||
| return decoder.decode(buffer) | ||
| } | ||
| export const arr2text = (buffer) => decoder.decode(buffer) | ||
| // sacrifice ~20% speed for bundle size | ||
| const encoder = new TextEncoder() | ||
| export const text2arr = string => { | ||
| return encoder.encode(string) | ||
| } | ||
| export const text2arr = string => encoder.encode(string) | ||
| export const arr2base = buffer => { | ||
| return encode(buffer) | ||
| export const arr2base = buffer => encode(buffer) | ||
| export const base2arr = str => new Uint8Array(decode(str)) | ||
| export const bin2hex = str => { | ||
| let res = '' | ||
| let c | ||
| let i = 0 | ||
| const len = str.length | ||
| while (i < len) { | ||
| c = str.charCodeAt(i++) | ||
| res += alphabet[c >> 4] | ||
| res += alphabet[c & 0xF] | ||
| } | ||
| return res | ||
| } | ||
| export const base2arr = str => { | ||
| return new Uint8Array(decode(str)) | ||
| const MAX_ARGUMENTS_LENGTH = 0x10000 | ||
| export const hex2bin = hex => { | ||
| const points = new Array(hex.length / 2) | ||
| for (let i = 0, l = hex.length / 2; i < l; ++i) { | ||
| points[i] = parseInt(hex.substr(i * 2, 2), 16) | ||
| } | ||
| if (points.length <= MAX_ARGUMENTS_LENGTH) return String.fromCharCode(...points) | ||
| let res = '' | ||
| let i = 0 | ||
| while (i < points.length) { | ||
| res += String.fromCharCode(...points.slice(i, i += MAX_ARGUMENTS_LENGTH)) | ||
| } | ||
| return res | ||
| } | ||
@@ -24,0 +47,0 @@ |
+5
-1
| { | ||
| "name": "uint8-util", | ||
| "version": "2.1.5", | ||
| "version": "2.1.6", | ||
| "description": "Fastest possible buffer-like utilities for uint8.", | ||
@@ -45,4 +45,8 @@ "main": "index.js", | ||
| "scripts": { | ||
| "bench-node-bin2hex": "node benchmark/bin2hex.js", | ||
| "bench-node-hex2bin": "node benchmark/hex2bin.js", | ||
| "bench-node-2text": "node benchmark/arr2text.js", | ||
| "bench-node-2arr": "node benchmark/text2arr.js", | ||
| "bench-browser-bin2hex": "airtap --preset local -- benchmark/bin2hex.js", | ||
| "bench-browser-hex2bin": "airtap --preset local -- benchmark/hex2bin.js", | ||
| "bench-browser-2text": "airtap --preset local -- benchmark/arr2text.js", | ||
@@ -49,0 +53,0 @@ "bench-browser-2arr": "airtap --preset local -- benchmark/text2arr.js", |
+1
-1
@@ -6,3 +6,3 @@ /* Common package for dealing with hex/string/uint8 conversions (and sha1 hashing) | ||
| */ | ||
| const alphabet = '0123456789abcdef' | ||
| export const alphabet = '0123456789abcdef' | ||
| const encodeLookup = [] | ||
@@ -9,0 +9,0 @@ const decodeLookup = [] |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
2765002
0.15%12
20%675
22.5%