Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ interface NewableFunction extends Function {
interface IArguments {}
interface RegExp {}

interface Map<K,V> {
interface Map<K, V> {
readonly size: i32;
has(key: K): bool;
set(key: K, value: V): this;
Expand All @@ -2406,10 +2406,10 @@ interface Map<K,V> {
toString(): string;
}

type MapInitialEntries<K,V> = {key: K, value:V}[]
type MapInitialEntries<K, V> = {key: K, value: V}[]

interface MapConstructor {
new <K, V>(entries?: MapInitialEntries<K,V>): Map<K,V>;
new <K, V>(entries?: MapInitialEntries<K, V> | null): Map<K, V>;
}

declare const Map: MapConstructor;
Expand Down
10 changes: 4 additions & 6 deletions std/assembly/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ export class Map<K,V> {
private entriesOffset: i32 = 0;
private entriesCount: i32 = 0;

constructor(initialEntries: MapInitialEntries<K,V> = []) {
let entriesLength = initialEntries.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz revert this cached entriesLength var and use instead initialEntries.length


if (entriesLength > 0) {
if (entriesLength >= this.entriesCapacity) this.bucketsMask = entriesLength;
constructor(initialEntries: MapInitialEntries<K,V> | null = null) {
if (initialEntries) {
Copy link
Member

@MaxGraey MaxGraey Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use early return:

Suggested change
if (initialEntries) {
if (!initialEntries || !initialEntries.length) return;

This will reduce one level of ident

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad for suggesting that

if (initialEntries.length >= this.entriesCapacity) this.bucketsMask = initialEntries.length;
this.rehash((this.bucketsMask << 1) | 1);

for (let i = 0; i < entriesLength; i++) {
for (let i = 0; i < initialEntries.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz cache initialEntries.length into var

let key = initialEntries[i].key;
let value = initialEntries[i].value;
let hashCode = HASH<K>(key);
Expand Down
Loading
Loading