Skip to content

Commit

Permalink
perf: Optimize BitflagsN Read & Write (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz authored Jan 10, 2024
1 parent c6906d7 commit 38e95b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/bitflags/bitflags16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export class BitFlags16<
I extends Record<string, number>,
O = OutputRecord<I>,
> extends SizedType<O> {
#record: I;
#recordEntries: Array<[string, number]>;

constructor(record: I) {
super(2, 2);
this.#record = record;
this.#recordEntries = Object.entries(record);
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O {
Expand All @@ -18,7 +18,7 @@ export class BitFlags16<
const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint16(options.byteOffset);
for (const { 0: key, 1: flag } of Object.entries(this.#record)) {
for (const { 0: key, 1: flag } of this.#recordEntries) {
returnObject[key] = (byteBag & flag) === flag;
}

Expand All @@ -36,7 +36,7 @@ export class BitFlags16<

let flags = 0;

for (const { 0: key, 1: flagValue } of Object.entries(this.#record)) {
for (const { 0: key, 1: flagValue } of this.#recordEntries) {
if (value[key as keyof O]) {
flags |= flagValue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/bitflags/bitflags32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export class BitFlags32<
I extends Record<string, number>,
O = OutputRecord<I>,
> extends SizedType<O> {
#record: I;
#recordEntries: Array<[string, number]>;

constructor(record: I) {
super(4, 4);
this.#record = record;
this.#recordEntries = Object.entries(record);
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O {
Expand All @@ -18,7 +18,7 @@ export class BitFlags32<
const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint32(options.byteOffset);
for (const { 0: key, 1: flag } of Object.entries(this.#record)) {
for (const { 0: key, 1: flag } of this.#recordEntries) {
returnObject[key] = (byteBag & flag) === flag;
}

Expand All @@ -36,7 +36,7 @@ export class BitFlags32<

let flags = 0;

for (const { 0: key, 1: flagValue } of Object.entries(this.#record)) {
for (const { 0: key, 1: flagValue } of this.#recordEntries) {
if (value[key as keyof O]) {
flags |= flagValue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/bitflags/bitflags64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export class BitFlags64<
I extends Record<string, bigint>,
O = OutputRecord<I>,
> extends SizedType<O> {
#record: I;
#recordEntries: Array<[string, bigint]>;

constructor(record: I) {
super(8, 8);
this.#record = record;
this.#recordEntries = Object.entries(record);
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O {
Expand All @@ -18,7 +18,7 @@ export class BitFlags64<
const returnObject: Record<string, boolean> = {};

const byteBag = dt.getBigUint64(options.byteOffset);
for (const { 0: key, 1: flag } of Object.entries(this.#record)) {
for (const { 0: key, 1: flag } of this.#recordEntries) {
returnObject[key] = (byteBag & flag) === flag;
}

Expand All @@ -36,7 +36,7 @@ export class BitFlags64<

let flags = 0n;

for (const { 0: key, 1: flagValue } of Object.entries(this.#record)) {
for (const { 0: key, 1: flagValue } of this.#recordEntries) {
if (value[key as keyof O]) {
flags |= flagValue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/bitflags/bitflags8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export class BitFlags8<
I extends Record<string, number>,
O = OutputRecord<I>,
> extends SizedType<O> {
#record: I;
#recordEntries: Array<[string, number]>;

constructor(record: I) {
super(1, 1);
this.#record = record;
this.#recordEntries = Object.entries(record);
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O {
Expand All @@ -18,7 +18,7 @@ export class BitFlags8<
const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint8(options.byteOffset);
for (const { 0: key, 1: flag } of Object.entries(this.#record)) {
for (const { 0: key, 1: flag } of this.#recordEntries) {
returnObject[key] = (byteBag & flag) === flag;
}

Expand All @@ -36,7 +36,7 @@ export class BitFlags8<

let flags = 0;

for (const { 0: key, 1: flagValue } of Object.entries(this.#record)) {
for (const { 0: key, 1: flagValue } of this.#recordEntries) {
if (value[key as keyof O]) {
flags |= flagValue;
}
Expand Down

0 comments on commit 38e95b9

Please sign in to comment.