binarius
Advanced tools
Comparing version 0.0.4 to 0.0.5
61
index.js
@@ -16,5 +16,3 @@ // fallback to using Number if BigInt is not available | ||
function normalizeFields(fields) { | ||
return typeof fields[0] === 'string' | ||
? fields.map(field => ({ name: field, size: 1 })) | ||
: fields; | ||
return typeof fields[0] === 'object' ? fields : fields.map(field => ({ name: field, size: 1 })); | ||
} | ||
@@ -83,9 +81,5 @@ | ||
constructor(data) { | ||
const { zero, fields } = this.constructor; | ||
const isBigInt = typeof zero === 'bigint'; | ||
this.value = Array.isArray(data) ? data.reduce((result, current, index) => { | ||
result <<= fields[index].size; | ||
result |= (isBigInt ? BigInt(current) : current); | ||
return result; | ||
}, zero) : data; | ||
const { isBigInt } = this.constructor; | ||
const value = Array.isArray(data) ? this.constructor.encode(data) : data; | ||
this.value = isBigInt ? BigInt(value) : value; | ||
} | ||
@@ -129,4 +123,4 @@ | ||
set(field, value = this.constructor.one) { | ||
const { offsets, one, masks } = this.constructor; | ||
if (typeof one === 'bigint') value = BigInt(value); | ||
const { offsets, masks, isBigInt } = this.constructor; | ||
if (isBigInt) value = BigInt(value); | ||
this.value = (this.value & ~(masks[field] << offsets[field])) | (value << offsets[field]); | ||
@@ -182,5 +176,42 @@ return this; | ||
toObject() { | ||
const { fields, masks } = this.constructor; | ||
return this.constructor.decode(this.value); | ||
} | ||
/** | ||
* Encodes a given list of numbers into a single number according to the schema. | ||
* | ||
* @param {Array<number>} data the list of numbers to encode | ||
* @returns {number} encoded number | ||
* @example | ||
* | ||
* const Person = BinariusFactory([{ name: 'age', size: 7}, { name: 'gender', size: 1}]); | ||
* Person.encode([20, 1]) | ||
* //=> 41 | ||
*/ | ||
static encode(data) { | ||
const { zero, fields, isBigInt } = this; | ||
let result = zero; | ||
for (let i = 0; i < data.length; i++) { | ||
const current = data[i]; | ||
result <<= fields[i].size; | ||
result |= (isBigInt ? BigInt(current) : current); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Decodes an encoded number into it's object representation according to the schema. | ||
* | ||
* @param {number} data encoded number | ||
* @returns {Object<string, number>} object representation | ||
* @example | ||
* | ||
* const Person = BinariusFactory([{ name: 'age', size: 7}, { name: 'gender', size: 1}]); | ||
* Person.decode(41); | ||
* //=> { age: 20, gender: 1 } | ||
*/ | ||
static decode(data) { | ||
const { fields, masks } = this; | ||
const result = {}; | ||
let { value } = this; | ||
let value = data; | ||
for (let i = fields.length - 1; i >= 0; i--) { | ||
@@ -197,3 +228,3 @@ const { name, size } = fields[i]; | ||
const size = fields[0].size + getOffset(fields, 0); | ||
const isBigInt = size > 32; | ||
const isBigInt = size > 31; | ||
const [zero, one, two] = isBigInt ? [BigInt(0), BigInt(1), BigInt(2)] : [0, 1, 2]; | ||
@@ -200,0 +231,0 @@ const [masks, offsets] = getMasks(fields, two, one); |
@@ -48,3 +48,3 @@ const binarius = require('./index'); | ||
BinariusNumber = binarius([{ name: 'age', size: 7 }, { name: 'gender', size: 1 }]); | ||
BinariusLargestNumber = binarius([{ name: 'age', size: 31 }, { name: 'gender', size: 1 }]); | ||
BinariusLargestNumber = binarius([{ name: 'width', size: 16 }, { name: 'height', size: 15 }]); | ||
BinariusBigInt = binarius([ | ||
@@ -62,2 +62,3 @@ { name: 'age', size: 7 }, | ||
expect(new BinariusNumber(41).value).toBe(41); | ||
expect(new BinariusLargestNumber([65535, 32767]).value).toBe(2147483647); | ||
expect(new BinariusBigInt(BigInt(1375759717)).value).toBe(BigInt(1375759717)); | ||
@@ -77,4 +78,4 @@ expect(new BinariusBits(5).value).toBe(5); | ||
expect(new BinariusNumber(41).get('age')).toBe(20); | ||
expect(new BinariusLargestNumber([20, 1]).get('age')).toBe(20); | ||
expect(new BinariusLargestNumber(41).get('age')).toBe(20); | ||
expect(new BinariusLargestNumber([65535, 32767]).get('width')).toBe(65535); | ||
expect(new BinariusLargestNumber([65535, 32767]).get('height')).toBe(32767); | ||
expect(new BinariusBigInt([20, 1, 3500, 5]).get('weight')).toBe(3500); | ||
@@ -90,3 +91,3 @@ expect(new BinariusBigInt(BigInt(1375759717)).get('weight')).toBe(3500); | ||
expect(new BinariusNumber([20, 1]).set('age', 30).get('age')).toBe(30); | ||
expect(new BinariusLargestNumber([20, 1]).set('age', 2147483647).get('age')).toBe(2147483647); | ||
expect(new BinariusLargestNumber([65535, 32760]).set('height', 32767).get('height')).toBe(32767); | ||
expect(new BinariusNumber([20, 0]).set('gender').get('gender')).toBe(1); | ||
@@ -112,2 +113,9 @@ expect(new BinariusNumber(41).set('gender', 0).get('gender')).toBe(0); | ||
describe('toValue', () => { | ||
it('returns value of an instance', () => { | ||
expect(new BinariusNumber([20, 1]).toValue()).toBe(41); | ||
expect(new BinariusBigInt([20, 1, 3500, 5]).toValue()).toBe(BigInt(1375759717)); | ||
expect(new BinariusBits([1, 0, 1]).toValue()).toBe(5); | ||
}); | ||
}); | ||
@@ -126,11 +134,3 @@ describe('toObject', () => { | ||
}); | ||
describe('toValue', () => { | ||
it('returns value of an instance', () => { | ||
expect(new BinariusNumber([20, 1]).toValue()).toBe(41); | ||
expect(new BinariusBigInt([20, 1, 3500, 5]).toValue()).toBe(BigInt(1375759717)); | ||
expect(new BinariusBits([1, 0, 1]).toValue()).toBe(5); | ||
}); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "binarius", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Store and operate on data in Numbers and BigInts for memory savings, performance, and fun.", | ||
@@ -20,2 +20,4 @@ "main": "index.js", | ||
"benchmark": "^2.1.4", | ||
"bitflags": "^1.0.0", | ||
"bitset": "^5.0.5", | ||
"codecov": "^3.1.0", | ||
@@ -25,3 +27,5 @@ "eslint": "^5.11.1", | ||
"eslint-plugin-import": "^2.14.0", | ||
"jest": "^23.6.0" | ||
"fast-bitset": "^1.3.2", | ||
"jest": "^23.6.0", | ||
"tiny-binary-format": "0.0.2" | ||
}, | ||
@@ -28,0 +32,0 @@ "scripts": { |
@@ -20,27 +20,42 @@ # Binarius | ||
Construct: | ||
Binarius Number x 29,866,474 ops/sec ±8.67% (57 runs sampled) | ||
Binarius BigInt x 674,095 ops/sec ±2.11% (83 runs sampled) | ||
binarius (Number) x 51,868,502 ops/sec ±9.35% (73 runs sampled) | ||
tiny-binary-format x 27,811,598 ops/sec ±9.24% (75 runs sampled) | ||
Binarius BigInt x 998,165 ops/sec ±8.40% (77 runs sampled) | ||
Get Field: | ||
Binarius Number x 8,643,094 ops/sec ±9.13% (59 runs sampled) | ||
Binarius BigInt x 617,815 ops/sec ±6.50% (73 runs sampled) | ||
binarius (Number) x 10,399,734 ops/sec ±9.35% (73 runs sampled) | ||
tiny-binary-format x 16,948,777 ops/sec ±9.69% (72 runs sampled) | ||
binarius (BigInt) x 969,008 ops/sec ±10.44% (64 runs sampled) | ||
Set Field: | ||
Binarius Number x 6,918,546 ops/sec ±8.78% (78 runs sampled) | ||
Binarius BigInt x 477,000 ops/sec ±5.86% (84 runs sampled) | ||
Construct for bit flags: | ||
Binarius x 8,019,238 ops/sec ±7.24% (47 runs sampled) | ||
parseInt x 9,128,021 ops/sec ±7.62% (61 runs sampled) | ||
Get bit flag: | ||
Binarius x 26,310,759 ops/sec ±10.21% (59 runs sampled) | ||
Bits x 40,664,455 ops/sec ±2.06% (86 runs sampled) | ||
Set bit flag: | ||
Binarius x 14,740,229 ops/sec ±9.67% (52 runs sampled) | ||
Bits x 30,816,599 ops/sec ±11.43% (51 runs sampled) | ||
binarius (Number) x 11,905,363 ops/sec ±9.10% (75 runs sampled) | ||
binarius (BigInt) x 577,570 ops/sec ±8.59% (81 runs sampled) | ||
Construct Small Bit Set: | ||
binarius x 12,680,530 ops/sec ±9.31% (70 runs sampled) | ||
parseInt x 11,408,948 ops/sec ±9.60% (61 runs sampled) | ||
fast-bitset x 2,008,394 ops/sec ±10.44% (60 runs sampled) | ||
bitflags x 1,882,259 ops/sec ±9.76% (60 runs sampled) | ||
Get Small Bit Set: | ||
binarius x 33,838,562 ops/sec ±9.15% (65 runs sampled) | ||
Vanilla x 39,430,233 ops/sec ±9.54% (64 runs sampled) | ||
fast-bitset x 1,600,432 ops/sec ±8.72% (71 runs sampled) | ||
bitflags x 1,492,410 ops/sec ±9.44% (64 runs sampled) | ||
Set Small Bit Set: | ||
binarius x 16,161,097 ops/sec ±10.18% (62 runs sampled) | ||
Vanilla x 69,220,698 ops/sec ±10.08% (64 runs sampled) | ||
fast-bitset x 1,955,982 ops/sec ±9.59% (59 runs sampled) | ||
bitflags x 1,883,057 ops/sec ±9.72% (60 runs sampled) | ||
Construct Large Bit Set: | ||
binarius (BigInt) x 15,429,294 ops/sec ±10.49% (63 runs sampled) | ||
parseInt x 6,422,370 ops/sec ±10.01% (66 runs sampled) | ||
fast-bitset x 1,853,205 ops/sec ±8.88% (60 runs sampled) | ||
bitflags x 1,675,325 ops/sec ±9.37% (65 runs sampled) | ||
Get Large Bit Set: | ||
binarius (BigInt) x 3,164,770 ops/sec ±9.84% (64 runs sampled) | ||
fast-bitset x 2,294,947 ops/sec ±9.02% (68 runs sampled) | ||
bitflags x 1,713,873 ops/sec ±9.45% (61 runs sampled) | ||
Set Large Bit Set: | ||
binarius (BigInt) x 1,351,004 ops/sec ±10.80% (66 runs sampled) | ||
fast-bitset x 1,849,869 ops/sec ±9.61% (63 runs sampled) | ||
bitflags x 1,900,251 ops/sec ±10.02% (60 runs sampled) | ||
``` | ||
## License | ||
MIT © [Maga D. Zandaqo](http://maga.name) |
17653
345
61
10