Skip to content

Commit

Permalink
chore: optimize: replace Array.isArrat with isArray where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Aug 14, 2023
1 parent 1c3194b commit dfe41c1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/resolver/ExpressionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExpressionObj, Expression, InnerExpression, ExpressionOperator } from '../api/expression'
import { compact, isObject } from './lib/lib'
import { compact, isArray, isObject } from './lib/lib'
import { Cache } from './lib/cache'
import { isNumericExpr } from './lib/expression'

Expand Down Expand Up @@ -129,7 +129,7 @@ export class ExpressionHandler {
public validateExpression(operatorList: Array<string>, expr0: Expression, breadcrumbs?: string): true {
if (!breadcrumbs) breadcrumbs = 'ROOT'

if (isObject(expr0) && !Array.isArray(expr0)) {
if (isObject(expr0)) {
const expr: ExpressionObj = expr0

if (expr.l === undefined)
Expand Down Expand Up @@ -159,7 +159,7 @@ export class ExpressionHandler {
private words2Expression(operatorList: Array<string>, words: Array<any>): Expression {
/* istanbul ignore if */
if (!words?.length) throw new Error('words2Expression: syntax error: unbalanced expression')
while (words.length === 1 && Array.isArray(words[0])) words = words[0]
while (words.length === 1 && words[0] !== null && isArray(words[0])) words = words[0]
if (words.length === 1) return words[0]

// Find the operator with the highest priority:
Expand Down
56 changes: 36 additions & 20 deletions src/resolver/ResolvedTimelineHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReferenceHandler, ValueWithReference } from './ReferenceHandler'
import { Expression } from '../api/expression'
import { ResolvedTimeline, ResolvedTimelineObject, TimelineObjectInstance } from '../api/resolvedTimeline'
import { Content, TimelineEnable, TimelineKeyframe, TimelineObject } from '../api/timeline'
import { assertNever, ensureArray, literal, pushToArray } from './lib/lib'
import { assertNever, ensureArray, isArray, literal, pushToArray } from './lib/lib'
import { InstanceHandler } from './InstanceHandler'
import {
getRefClass,
Expand Down Expand Up @@ -232,7 +232,10 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
pushToArray<Reference>(directReferences, lookupRepeating.allReferences)

let lookedupRepeating: ValueWithReference | null
if (Array.isArray(lookupRepeating.result)) {
if (lookupRepeating.result === null) {
// Do nothing
lookedupRepeating = null
} else if (isArray(lookupRepeating.result)) {
if (lookupRepeating.result.length === 0) {
lookedupRepeating = null
} else if (lookupRepeating.result.length === 1) {
Expand Down Expand Up @@ -266,7 +269,10 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
const lookupWhile = this.reference.lookupExpression(obj, whileExpr, 'start')
pushToArray<Reference>(directReferences, lookupWhile.allReferences)

if (Array.isArray(lookupWhile.result)) {
if (lookupWhile.result === null) {
// Do nothing
enableInstances = []
} else if (isArray(lookupWhile.result)) {
enableInstances = lookupWhile.result
} else if (lookupWhile.result !== null) {
enableInstances = [
Expand Down Expand Up @@ -296,7 +302,9 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
// const endEvents: EventForInstance[] = []
let iStart = 0
let iEnd = 0
if (Array.isArray(lookedupStarts)) {
if (lookedupStarts === null) {
// Do nothing
} else if (isArray(lookedupStarts)) {
for (let i = 0; i < lookedupStarts.length; i++) {
const instance = lookedupStarts[i]
const eventId = `${obj.id}_${iStart++}`
Expand All @@ -307,7 +315,7 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
references: instance.references,
})
}
} else if (lookedupStarts !== null) {
} else {
events.push({
time: lookedupStarts.value,
value: true,
Expand Down Expand Up @@ -340,7 +348,9 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
? this.reference.applyParentInstances(parentInstances, lookupEnd.result)
: lookupEnd.result

if (Array.isArray(lookedupEnds)) {
if (lookedupEnds === null) {
// Do nothing
} else if (isArray(lookedupEnds)) {
for (let i = 0; i < lookedupEnds.length; i++) {
const instance = lookedupEnds[i]
events.push({
Expand All @@ -350,7 +360,7 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
references: instance.references,
})
}
} else if (lookedupEnds !== null) {
} else if (lookedupEnds) {
events.push({
time: lookedupEnds.value,
value: false,
Expand All @@ -372,20 +382,26 @@ export class ResolvedTimelineHandler<TContent extends Content = Content> {
pushToArray<Reference>(directReferences, lookupDuration.allReferences)

let lookedupDuration = lookupDuration.result
if (Array.isArray(lookedupDuration) && lookedupDuration.length === 1) {
lookedupDuration = literal<ValueWithReference>({
value: lookedupDuration[0].start,
references: lookedupDuration[0].references,
})

if (lookedupDuration === null) {
// Do nothing
} else if (isArray(lookedupDuration)) {
if (lookedupDuration.length === 1) {
lookedupDuration = literal<ValueWithReference>({
value: lookedupDuration[0].start,
references: lookedupDuration[0].references,
})
} else if (lookedupDuration.length === 0) {
lookedupDuration = null
} else {
// Lookup rendeded multiple durations.
// This is unsupported at the moment, but could possibly be added in the future.
/* istanbul ignore next */
throw new Error(`lookedupDuration should never return an array for .duration lookup`)
}
}
if (Array.isArray(lookedupDuration) && !lookedupDuration.length) lookedupDuration = null

if (Array.isArray(lookedupDuration)) {
// Lookup rendeded multiple durations.
// This is unsupported at the moment, but could possibly be added in the future.
/* istanbul ignore next */
throw new Error(`lookedupDuration should never return an array for .duration lookup`)
} else if (lookedupDuration !== null) {

if (lookedupDuration !== null) {
if (lookedupRepeating !== null && lookedupDuration.value > lookedupRepeating.value) {
// Cap duration to repeating duration
lookedupDuration.value = lookedupRepeating.value
Expand Down
20 changes: 12 additions & 8 deletions src/resolver/StateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TimelineState,
} from '../api'
import { instanceIsActive } from './lib/instance'
import { clone, isObject } from './lib/lib'
import { clone, isArray, isObject } from './lib/lib'
import { tic } from './lib/performance'
import { objHasLayer } from './lib/timeline'

Expand Down Expand Up @@ -74,13 +74,17 @@ export class StateHandler {
public static applyKeyframeContent(parentContent: Content, keyframeContent: Content): void {
const toc = tic(' applyKeyframeContent')
for (const [attr, value] of Object.entries<any>(keyframeContent)) {
if (Array.isArray(value)) {
if (!Array.isArray(parentContent[attr])) parentContent[attr] = []
this.applyKeyframeContent(parentContent[attr], value)
parentContent[attr].splice(value.length, Infinity)
} else if (isObject(value)) {
if (!isObject(parentContent[attr]) || Array.isArray(parentContent[attr])) parentContent[attr] = {}
this.applyKeyframeContent(parentContent[attr], value)
if (isObject(value)) {
if (isArray(value)) {
// Value is an array
if (!Array.isArray(parentContent[attr])) parentContent[attr] = []
this.applyKeyframeContent(parentContent[attr], value)
parentContent[attr].splice(value.length, Infinity)
} else {
// Value is an object
if (!isObject(parentContent[attr]) || Array.isArray(parentContent[attr])) parentContent[attr] = {}
this.applyKeyframeContent(parentContent[attr], value)
}
} else {
parentContent[attr] = value
}
Expand Down
4 changes: 4 additions & 0 deletions src/resolver/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export function isEmpty(obj: object): boolean {
export function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
/**
* Slightly faster than Array.isArray().
* Note: Ensure that the value provided is not null!
*/
export function isArray(arg: object | any[]): arg is any[] {
// Fast-path optimization: checking for .length is faster than Array.isArray()

Expand Down

0 comments on commit dfe41c1

Please sign in to comment.