Skip to content

Commit

Permalink
[perf | size] simplify array iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Dec 5, 2023
1 parent 7c86dbd commit 1b977e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
16 changes: 8 additions & 8 deletions packages/@glimmer/runtime/lib/compiled/expressions/concat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import type { Dict, Maybe } from '@glimmer/interfaces';
import type { Reference } from '@glimmer/reference';
import { createComputeRef, valueForRef } from '@glimmer/reference';
import { enumerate } from '@glimmer/util';

export function createConcatRef(partsRefs: Reference[]) {
return createComputeRef(() => {
let parts = new Array<string>();
const parts: string[] = [];

for (const [i, ref] of enumerate(partsRefs)) {
let value = valueForRef(ref) as Maybe<Dict>;
for (const ref of partsRefs) {
const value = valueForRef(ref);

if (value !== null && value !== undefined) {
parts[i] = castToString(value);
parts.push(castToString(value));
}
}

Expand All @@ -23,8 +21,10 @@ export function createConcatRef(partsRefs: Reference[]) {
});
}

function castToString(value: Dict) {
if (typeof value.toString !== 'function') {
function castToString(value: string | object) {
if (typeof value === 'string') {
return value;
} else if (typeof value.toString !== 'function') {
return '';
}

Expand Down
7 changes: 1 addition & 6 deletions packages/@glimmer/runtime/lib/compiled/opcodes/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,7 @@ function mergeClasses(classes: (string | Reference)[]): string | Reference<unkno
}

function allStringClasses(classes: (string | Reference<unknown>)[]): classes is string[] {
for (let i = 0; i < classes.length; i++) {
if (typeof classes[i] !== 'string') {
return false;
}
}
return true;
return classes.every((c) => typeof c === 'string');
}

function setDeferredAttr(
Expand Down
7 changes: 3 additions & 4 deletions packages/@glimmer/runtime/lib/compiled/opcodes/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ APPEND_OPCODES.add(Op.FlushElement, (vm) => {
APPEND_OPCODES.add(Op.CloseElement, (vm) => {
let modifiers = vm.elements().closeElement();

if (modifiers) {
if (modifiers !== null) {
modifiers.forEach((modifier) => {
vm.env.scheduleInstallModifier(modifier);
let { manager, state } = modifier;
let d = manager.getDestroyable(state);
const d = modifier.manager.getDestroyable(modifier.state);

if (d) {
if (d !== null) {
vm.associateDestroyable(d);
}
});
Expand Down
12 changes: 2 additions & 10 deletions packages/@glimmer/runtime/lib/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,13 @@ export function isScopeReference(s: ScopeSlot): s is Reference {

export class PartialScopeImpl implements PartialScope {
static root(self: Reference<unknown>, size = 0, owner: Owner): PartialScope {
let refs: Reference<unknown>[] = new Array(size + 1);

for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
let refs: Reference<unknown>[] = new Array(size + 1).fill(UNDEFINED_REFERENCE);

return new PartialScopeImpl(refs, owner, null, null, null).init({ self });
}

static sized(size = 0, owner: Owner): Scope {
let refs: Reference<unknown>[] = new Array(size + 1);

for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
let refs: Reference<unknown>[] = new Array(size + 1).fill(UNDEFINED_REFERENCE);

return new PartialScopeImpl(refs, owner, null, null, null);
}
Expand Down

0 comments on commit 1b977e9

Please sign in to comment.