Skip to content Skip to footer

Servoy Tutorial: Function Memoization

Servoy Tutorial: Function Memoization
Servoy Tutorial Photo Credit: Reigh LeBlanc via Compfight

This is an object-oriented Servoy tutorial on how to use function memoization with Servoy. Function memoization uses a local object to build a cache and can improve code performance, avoiding methods from calculating the same thing over and over again. Suppose you have a function that takes in an item id and will then perform a bunch of calculations. If you performed the calculation for a given item already, function memoization allows you to grab the previously computed result and return it, instantly, rather than running through hundreds of lines of code that will compute the same result.

Here is an example of how to add function memoization to an existing method:

/**
 * A very long and computational heavy method
 * 
 * @param {String} sItemUUID - imaginary item uuid
 * @param {String} [subMethod] - submethod to run (clearCache)
 */
function computeItemStatistics(sItemUUID, subMethod){
	// If a cache has not been setup, set it up now
	if (!this.cache){
		this.cache = {};
	}

	// This allows us to dump the cache
	if (subMethod && subMethod === "clearCache"){
		this.cache = null;
		application.output("cache cleared");
		return null;
	}

	// Return the info if the item is already in the cache
	if (sItemUUID in this.cache){
		application.output("Returning item from cache");
		// returning an object containing the computed data
		return this.cache[sItemUUID]; 
	}

	/*
	 * Lots of complex code	
	 */

	// At the end of the method, save the item info to the cache
	application.output("Adding " + sItemUUID + " to cache");
	this.cache[sItemUUID] = {
		computedValue1: null, // store your values here
		computedValue2: null,
		computedValue3: null // etc.
	};
	// method always returns the same object structure with computed results
	return this.cache[sItemUUID]; 
}

Everything in JavaScript is an object, and functions are no exception. Function memoization takes advantage of this, retaining the cached data even after the function has returned. Each time the function is called, the cache is there and accessible to all code in your function.

I want to mention that you should never use more than one cache per function, because function memoization stores the values in “this”, which points to the object that the function belongs to. So you can only have one cache active per function, but you can store a lot of information in that cache, since the cache is an object, and you can store more objects inside it.

Function memoization is a great way to quickly convert some inefficient legacy code and improve performance.

Leave a comment

OUR SERVICES

AI Integration and Agent Development

We design, prototype, and deploy AI agents that streamline operations and enhance decision-making. Our expertise includes:

  • Custom AI Agent Development: Crafting autonomous systems using tools like n8n, Flowise, and PydanticAI.
  • Workflow Automation: Reducing operational inefficiencies through scalable, automated solutions.
  • Machine Learning Integration: Building predictive models to uncover actionable insights.
  • AI Adoption Consulting: Guiding businesses through the process of integrating AI technologies to achieve their strategic goals.

Expert Prompt Engineering

Effective prompts unlock the full potential of AI systems. We specialize in:

  • Custom Prompt Design: Developing precise and context-aware inputs to guide AI systems effectively.
  • Advanced Prompt Patterns: Using techniques like persona-based prompts, two-shot prompting, and reflection prompts for superior outcomes.
  • Industry-Specific Solutions: Tailoring prompts to solve challenges in fields like finance, healthcare, retail, and more.

Data Solutions and Insights

Unlock the power of your data with our comprehensive solutions:

  • Data Mining and Analysis: Extracting valuable insights to drive informed business decisions.
  • Data Visualization: Creating clear and interactive dashboards for impactful storytelling.
  • Advanced SQL Expertise: Leveraging MS SQL and PostgreSQL to build robust, scalable database solutions.

AI-Powered Project Management

Managing complex AI projects from start to finish:

  • End-to-End Project Management: From initial strategy to deployment, ensuring seamless execution.
  • Cross-Functional Collaboration: Bridging technical and business teams to align project goals.
  • Strategic Planning: Delivering AI solutions that are on time, within budget, and aligned with business objectives.

Why Choose Dotzlaw Consulting?

With over 20 years of experience in AI, software development, and workflow optimization, we combine technical expertise with business insight to deliver transformative results. Let us help you unlock the full potential of AI to drive growth and innovation.

Ready to Get Started?

Let’s collaborate to achieve your goals. Contact us today to explore how AI can transform your business.


Our Skills:

[bra_graph_container]
[bra_graph Title=’Artificial Intelligence (AI) Consulting’ Percent=’95’]
[bra_graph Title=’Prompt Engineering’ Percent=’90’]
[bra_graph Title=’Workflow Automation’ Percent=’90’]
[bra_graph Title=’Python’ Percent=’90’]
[bra_graph Title=’SQL’ Percent=’85’]
[bra_graph Title=’Data Mining and Visualization’ Percent=’80’]
[bra_graph Title=’Machine Learning’ Percent=’80’]
[bra_graph Title=’Servoy’ Percent=’99’]
[/bra_graph_container]

Core AI and Consulting Skills

[bra_list style=’arrow-list’]

  • Artificial Intelligence (AI) Consulting: Delivering transformative AI solutions tailored to business challenges.
  • Machine Learning: Expertise in designing, training, and deploying machine learning models.
  • Generative AI Workflows: Optimizing workflows with generative AI technologies for scalability and efficiency.
  • Predictive Analytics: Leveraging data to forecast trends and improve decision-making.
  • Data Mining and Analysis: Uncovering actionable insights from complex datasets.

[/bra_list]

Workflow and System Modernization

[bra_list style=’arrow-list’]

  • Workflow Automation (n8n, Flowise): Automating processes with cutting-edge tools to streamline operations.
  • Legacy System Modernization: Upgrading outdated systems using modern frameworks and AI-driven approaches.
  • AI Agent Development: Prototyping and scaling AI agents from concept to production.
  • Scalable Workflow Design: Crafting workflows optimized for efficiency and growth.
  • Business Process Optimization: Transforming processes to improve productivity and reduce costs.

[/bra_list]

Technical Expertise

[bra_list style=’arrow-list’]

  • Python, Java, JavaScript Programming: Proficiency in versatile programming languages for AI and software development.
  • Expert Servoy Programmer: Advanced expertise in Servoy development for specialized client needs.
  • AI Agent Prototyping with n8n and Flowise: Rapid prototyping using state-of-the-art AI agent development tools.
  • AI Agent Scalable Production Solutions with PydanticAI: Delivering robust, production-ready AI solutions.
  • Advanced AI Prompt Engineering: Crafting high-performing prompts for generative AI applications.

[/bra_list]

[bra_button text=’Contact Us’ url=’https://dotzlaw.com/clone/contact-me’ target=’_self’ size=’medium’ style=’rounded’ color=’tealgreen’]