Skip to content

Commit 3fe91cc

Browse files
committed
ES2015 set/map usage, removes a few bytes
1 parent b649d92 commit 3fe91cc

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Diff for: src/index.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface Styles {
2121
/**
2222
* Quick dictionary lookup for unit-less numbers.
2323
*/
24-
const CSS_NUMBER = Object.create(null) as Record<string, true>;
24+
const CSS_NUMBER = new Set<string>();
2525

2626
/**
2727
* CSS properties that are valid unit-less numbers.
@@ -79,7 +79,7 @@ const CSS_NUMBER_KEYS = [
7979
// Add vendor prefixes to all unit-less properties.
8080
for (const property of CSS_NUMBER_KEYS) {
8181
for (const prefix of ["-webkit-", "-ms-", "-moz-", "-o-", ""]) {
82-
CSS_NUMBER[prefix + property] = true;
82+
CSS_NUMBER.add(prefix + property);
8383
}
8484
}
8585

@@ -156,7 +156,7 @@ type Tuple<T> = [string, T];
156156
* Transform a style string to a CSS string.
157157
*/
158158
function tupleToStyle([name, value]: Tuple<NonNullable<PropertyValue>>) {
159-
if (typeof value === "number" && value && !CSS_NUMBER[name]) {
159+
if (typeof value === "number" && value && !CSS_NUMBER.has(name)) {
160160
return `${name}:${value}px`;
161161
}
162162

@@ -299,15 +299,15 @@ export class Cache<T extends Container<any>> {
299299

300300
protected sheet: string[] = [];
301301
protected children: T[] = [];
302-
protected counters: Record<string, number | undefined> = Object.create(null);
302+
protected counters = new Map<string, number>();
303303

304304
constructor(public changes?: Changes) {}
305305

306306
add(style: T): void {
307307
const id = style.cid();
308-
const count = this.counters[id] || 0;
308+
const count = this.counters.get(id) ?? 0;
309309

310-
this.counters[id] = count + 1;
310+
this.counters.set(id, count + 1);
311311

312312
if (count === 0) {
313313
const item = style.clone();
@@ -332,16 +332,14 @@ export class Cache<T extends Container<any>> {
332332

333333
remove(style: T): void {
334334
const id = style.cid();
335-
const count = this.counters[id];
335+
const count = this.counters.get(id);
336336

337337
if (count) {
338-
this.counters[id] = count - 1;
339-
340338
const index = this.children.findIndex((x) => x.cid() === id);
341339

342340
if (count === 1) {
343341
const item = this.children[index];
344-
delete this.counters[id];
342+
this.counters.delete(id);
345343
this.children.splice(index, 1);
346344
this.sheet.splice(index, 1);
347345
this.changeId++;
@@ -350,6 +348,7 @@ export class Cache<T extends Container<any>> {
350348
const item = this.children[index] as T & Cache<any>;
351349
const prevChangeId = item.changeId;
352350

351+
this.counters.set(id, count - 1);
353352
item.unmerge(style);
354353

355354
if (item.changeId !== prevChangeId) {

0 commit comments

Comments
 (0)