Computed properties

Computed properties in Aurelia allow you to create derived values that automatically update when their dependencies change. They're implemented using JavaScript getter methods and can be optimized using the @computedFrom decorator for enhanced performance.

Understanding Computed Properties

Computed properties are JavaScript getter methods that derive their values from other properties. Unlike regular properties, they're calculated on-demand and can automatically update when their underlying dependencies change.

Basic Computed Property Example

export class Person {
  firstName = 'John';
  lastName = 'Doe';

  get fullName() {
    console.log('Calculating full name...'); // This runs every time fullName is accessed
    return `${this.firstName} ${this.lastName}`;
  }
}
<template>
  <div>
    <p>Full Name: ${fullName}</p>
    <input value.bind="firstName" placeholder="First Name">
    <input value.bind="lastName" placeholder="Last Name">
  </div>
</template>

In this example, fullName automatically updates whenever firstName or lastName changes, and the UI reflects these changes immediately.

Performance Considerations

The Dirty Checking Problem

By default, Aurelia uses "dirty checking" to detect changes in computed properties. This means:

  • Aurelia periodically checks property values (approximately every 120ms)

  • Getter functions are executed multiple times during each check cycle

  • Complex calculations can significantly impact performance

  • The system doesn't know which properties the getter depends on

Dirty Checking Example

Without optimization, this getter runs multiple times per second, causing performance issues.

Optimizing with @computedFrom

The @computedFrom decorator solves the dirty checking performance problem by explicitly declaring dependencies.

Importing and Basic Usage

Benefits of @computedFrom

  • Eliminates dirty checking: Only recalculates when dependencies change

  • Improves performance: Especially beneficial for complex calculations

  • Explicit dependencies: Makes code more maintainable and predictable

  • Automatic optimization: Aurelia's binding system becomes more efficient

Advanced @computedFrom Usage

Multiple Dependencies

Nested Property Dependencies

For complex object structures, you can specify nested property paths:

Array Dependencies and Collection Changes

When depending on arrays, @computedFrom observes the array reference, not individual items:

Complex Computed Property Examples

Financial Calculator

Data Analysis Dashboard

Working with Observable Properties

Combine @computedFrom with @observable for reactive computed properties:

Best Practices

1. Always Use @computedFrom for Non-Trivial Getters

2. Be Explicit About All Dependencies

3. Use Meaningful Names

4. Chain Computed Properties When Appropriate

Common Pitfalls and Solutions

1. Forgetting Dependencies

Problem: Computed property doesn't update when it should

2. Array Item Property Changes

Problem: Computed property doesn't update when array item properties change

3. Overly Complex Dependencies

Problem: Too many dependencies make code hard to maintain

Performance Monitoring

To verify that @computedFrom is working correctly, you can add logging:

Summary

Computed properties are a powerful feature in Aurelia that enable reactive, derived data. Key takeaways:

  • Use @computedFrom for any non-trivial getter method

  • Explicitly declare all dependencies to ensure proper updates

  • Break complex calculations into smaller, composable computed properties

  • Be aware of array dependency limitations

  • Monitor performance to ensure optimizations are working

By following these patterns, you'll create maintainable, performant applications with reactive computed properties that automatically keep your UI in sync with your data.

Last updated

Was this helpful?