Logo

dev-resources.site

for different kinds of informations.

Flipping Data Structures to optimize performance 🚀

Published at
9/2/2024
Categories
javascript
optimization
datastructures
Author
slobodan4nista
Author
14 person written this
slobodan4nista
open
Flipping Data Structures to optimize performance 🚀

What do I mean

When we want a high performance system, we can greatly benefit by thinking in terms of efficient data organization. When we have multilayered maps or hash tables, sometimes a simple reorientation of data is all that we need.

In my ECS system, I have:

  • _entities that map id -> Entity(id)
  • _components that map Entity.id -> Component.Type -> Set(Component())

So when we query for all Entities that have a Component of a specific type, we list all entities and then filter out those that do not have a Component.Type entry or where the set size is 0.

This works well enough to a certain point, but we can observe that this algorithm performs similarly when searching for both common and rare components, even though searching for a rare component should, intuitively be faster.

One optimization we can implement is to flip the _component map Entity.id and Component.Type:

_components that map Component.Type -> Entity.id -> Set(Component())

The benefit is that when a component is rare, we only have to traverse a short list to check if the set is empty and filter those entities accordingly. This could be further improved by deleting the entity entry for a type when the set is empty. This way, our query can simply return a set.

Here are the results of this optimization for 100_000 eateries and 600 rare Component instances:

queryCommon: 51.256ms -> 16.161ms

queryRare: 58.081ms -> 0.346ms

getComponentsCommon: 37.668ms -> 34.671ms

getComponentsRare: 21.635ms -> 9.959ms
Enter fullscreen mode Exit fullscreen mode

Conclusion

By reorganizing how components are mapped within the ECS, we can significantly improve the performance of queries, especially for rare components. This simple yet effective change allows the system to handle query more efficiently.

Hope this helps developers not to forget to handle their data appropriately even when using something like JavaScript.

optimization Article's
30 articles in total
Favicon
How to apply Image optimization For Your Website
Favicon
Optimizing AWS Lambda Performance with Node.js: Minimizing Cold Start Latency
Favicon
Optimizing AWS Lambda Performance with Node.js: Minimizing Cold Start Latency
Favicon
7 Practical Tips to Minimize Your JavaScript Bundle Size
Favicon
How Can You Optimize MySQL Performance for High-Load Applications?
Favicon
Understanding String Interning in C#
Favicon
Comparing Debouncing and Cancellation Token: When to Use Each in Web Applications
Favicon
optifast. A Pc Optimization Software for everyone !
Favicon
7.Performance Optimization: Understanding Change Detection(Zone.js | default | onPush), Lazy Loading, Track By in ngFor
Favicon
This is how to Optimize React Apps for Performance
Favicon
AWS Cost Optimization Tools and Strategies
Favicon
A Beginner's Guide On How to Optimize Your Code (with JavaScript)
Favicon
React Flow(xyFlow) Optimization
Favicon
How I.T Dolphins Ensures Customer Satisfaction
Favicon
Designing robust and scalable relational databases: A series of best practices.
Favicon
Introducing Multi-Armed Bandits in GrowthBook
Favicon
Scaling the Outbox Pattern (2B+ messages per day)
Favicon
Why Hashed OTP Tokens Are Better Than Storing Them in a Database
Favicon
Improving Core Web Vitals for Modern Web Development: A Guide to Faster Sites
Favicon
Implementing computed gotos in C++
Favicon
Optimizing +200 Pipelines of a Monorepo
Favicon
Implementing an Intermediate Representation for ArkScript
Favicon
How I Implemented Full-Text Search On My Website
Favicon
Optimizing Laravel Performance: Addressing Slowdowns with Large Datasets
Favicon
Goal Programming: Balancing Multiple Objectives with Flexibility
Favicon
SWAP: Guide about swap and swappiness
Favicon
The HTML History and Optimization Cheat Sheet
Favicon
10 Essential Tips for Optimizing React Applications
Favicon
Flipping Data Structures to optimize performance 🚀
Favicon
Optimizing React Component Performance with Memoization

Featured ones: