Ways to boost the smoothlife performance and framerate in p5js

I have a NextJS project using p5js deployed on .

This project is an implementation of , which involves a cellular automata generalized on a continuous domain. Currently, it runs at around 10 to 14 frames per second and I aim to increase this.

You can find the source file with the logic here: https://github.com/JaylenLuc/ArtificalLife/blob/main/artificial_life/pages/smoothlife/smoothLife.tsx

export default function P5Sketch () {
    // Code logic goes here
}

How can I boost the framerate of my project without relying on shaders?

Answer №1

Improving performance is mostly affected by enlarging the radius and board size.

Your function fillingIntegralN_M seems to perform a 2D convolution. Am I correct?

When dealing with large kernels, especially a radius of a few grid units, using a fast Fourier transform (FFT) can significantly improve efficiency.

The approach involves precomputing FFTs for inner and outer "filling integral" kernels, where cells within specified radii have values of 1 or normalized factors if optimization is desired, while those outside are set to 0. Applying proper boundary treatments for cells at the edges adheres to the cyclic nature of the domain.

During each simulation iteration, the FFT of the grid is multiplied element-wise with these precomputed kernel FFTs. The inverse FFT of resulting grids provides updated m and n values per point for subsequent grid updates.

If your update rule were entirely linear, computations could be performed primarily in "Fourier space," toggling back occasionally to visually display the grid. However, since SmoothLife follows a non-linear update rule, constant transitioning between spaces is necessary leveraging optimized FFT implementations for fast execution.


In regard to JavaScript FFT implementation, building one from scratch as an initial step might divert focus to unnecessary complexities. Opting for existing alternatives like JavaScript FFT libraries such as fft.js, jsfft, or utilizing functions like math.fft in math.js would ensure both accurate implementation and speeder performance.

Moreover, consider exploring additional resources like p5.js' FFT module although primarily designed for audio applications. Its applicability for acquiring "raw" FFT of numerical data may require further insight due to certain limitations on input/output sizes affecting grid dimensions.


Addendum: Below is snippet demonstrating FFT convolution using jsfft library tailored to complement the SmoothLife algorithm. Although inspired by the referenced paper, slight modifications may be needed for alignment with personal code structure.

// Implementing JS code here...
<div id="smoothlife"></div>
<div id="fps"></div>

Running this example on my laptop achieved approximately 20 FPS with a grid measuring 512 by 256 points. Incidentally, optimizing it further could potentially enhance performance outcomes. Nonetheless, maintaining power-of-2 grid dimensions seemingly boosts FFT code execution efficiency.

Note that despite employing a single-dimensional FFT instead of a genuine 2D FFT, the choice doesn't impair convolution results as long as consistency prevails. As arrays storing two-dimensional grids mimic one-dimensional structures, deploying a 1D FFT simplifies coding and possibly enhances speeds.

Still, achieving classic SmoothLife dynamics similar to illustrated videos, even with identical parameter setups, remains elusive reflecting potential undiscovered bugs. Further comparisons with other implementations or analysis of specific integrations used may uncover discrepancies impacting outcome variations.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to put into practice a Calendar system?

What is the best way to integrate a Calendar in Angular2 using typescript? Are there any specific directives that need to be imported? ...

What is the process for importing a JSON file into a TypeScript script?

Currently, I am utilizing TypeScript in combination with RequireJS. In general, the AMD modules are being generated flawlessly. However, I have encountered a roadblock when it comes to loading JSON or any other type of text through RequireJS. define(["jso ...

What is preventing type-graphql from automatically determining the string type of a class property?

I have a custom class named Foo with a property called bar that is of type string. class Foo { bar: string } When I use an Arg (from the library type-graphql) without explicitly specifying the type and set the argument type to string, everything works ...

How can you obtain the userID in the Next.js 13 application directory on the server side using next-auth?

Currently, my setup includes: nextJS: 13.1.6 nextAuth 4.19.2 The application is utilizing the new app/ directory, currently in its Beta stage. An issue I am facing involves retrieving user information on a page and performing logic based on that ...

How to identify alterations in user input within Angular?

I need assistance with my search input functionality. I want to ensure that the this.searchProperties.emit is only triggered when the user interacts with the input field by touching it or making an input. The current issue is that the emit function gets ca ...

In Typescript, we can use a class to encapsulate another class and then return a generic result

Trying to wrap my head around the concept of generic classes, and now I need to dynamically create another class. However, I am uncertain about how to go about it. Class A {} Class B<T> { Return T } Const c = B(A); // which is T More context on w ...

The use of p-message in Angular's PrimeNg library is not permitted

Hey there, I'm having a bit of trouble with the p-message Tag in Angular. I believe I've imported it correctly as shown below. import { MessageModule } from 'primeng/message'; imports: [ .... MessageModule, ... In the ...

What should I do to resolve the error message TypeError: _components_firebase_Firebase__WEBPACK_IMPORTED_MODULE_2__.default.auth is not a valid function?

I have implemented Firebase with next.js and organized my files as shown below. However, I am encountering an issue with using the firebase client side SDK during the sign-up process. Firebase.js is where the firebase app is initialized import firebase fr ...

Observing the completion of a subscriber function

Is there a more streamlined way to determine if the subscriber has finished executing or return something and catch it up-stream? Consider the following code snippets: this._subscriptions.push(this._client .getCommandStream(this._command) // R ...

Issues with navigation in React Native Typescript

Currently, I am in the process of developing a new React Native Expo project utilizing TypeScript. I have been attempting to configure navigation following the guidance provided in React Native's TypeScript documentation. However, upon running and sim ...

Tips for creating a vue-cli application that can be customized post-build:

I have a functioning vue-cli application that I can easily build. However, I now need to create a single deployable build that can be used on multiple servers. The challenge is that depending on the server, I will need to adjust some basic variables such a ...

Enhance your TypeScript skills by leveraging types on the call() method in redux-saga

Is there a way to specify the types of a function using call()? Consider this function: export function apiFetch<T>(url: string): Promise<T> { return fetch(url).then(response => { if (!response.ok) throw new Error(r ...

When running the command "npx create-next-app@latest --ts," be aware that there are 3 high severity vulnerabilities present. One of the vulnerabilities is that "node-fetch

Just set up a fresh project with Next.js and TypeScript by following the documentation using npx create-next-app@latest --ts. Encountering multiple high severity vulnerabilities despite running npm audit fix --force, which actually adds more vulnerabiliti ...

Executing functions in Vue TypeScript during initialization, creation, or mounting stages

Just a few hours ago, I kicked off my Vue TypeScript project. I've successfully configured eslint and tslint rules to format the code as desired, which has left me quite pleased. Now, I'm curious about how to utilize the created/mounted lifecycl ...

Tips for preventing CORS and SSL issues when using localhost

Attempting to log in with Google on my Svelte app involves sending a request to an Express server. However, I encounter different errors on different browsers. On Firefox, I receive a Cross-Origin Request Blocked: The Same Origin Policy disallows reading t ...

What is the best way to retrieve the current value of a React useState hook within a setInterval function while using Highcharts

import Error from 'next/error' import React, { useState, useEffect } from 'react' import Highcharts from 'highcharts' import HighchartsReact from 'highcharts-react-official' function generateChart() { const [co ...

"Utilize Typescript to dynamically check data types during runtime and receive alerts for any potential

INTRODUCTION I am currently working with Angular 6. In Angular, typescript is utilized to allow you to specify the data type of your function's arguments: public fun1(aaa: number) { console.log(aaa); } If I attempt to call fun1 with a parameter ...

Creating a personal TypeScript language service extension in Visual Studio Code

Currently, I am working on developing a TSserver plugin in VSCode and struggling to get the server to load my plugin. I have experimented with setting the path in tsconfig.json to both a local path and a path to node_modules, but it still isn't worki ...

Using a custom font with Next.js and Tailwind: Font applied successfully but not displaying correctly

In my project with Next.js (9.4.4) and Tailwind.css (1.4.6), I am incorporating a custom font named SpaceGrotesk. To ensure its functionality, I stored the font files in public/fonts/spaceGrotesk, and then adjusted my configurations as below: // next.confi ...

Updating an image without modifying the URL does not result in the image being updated

Currently, I am using Next.js 14 and have it hosted on Vercel. My database is set up with Supabase, and for DNS management, I rely on Cloudflare. The website I run contains images, and the image URLs are fetched from my Supabase DB. Typically, these URLs ...