Most optimized way to create multiple objects with several operations

626 views Asked by At

So I've been looking into factory functions and classes, looking for the most optimized way of creating multiple objects with several functions/operations each.

Let's say I am doing a TODO list, every task I create has 7 parameters and even more operations but mainly getters and setters.

If I create each of these tasks using a factory function every object created will have almost a dozen functions in memory for each of the tasks created right?

Since we have Object.create() to create an object as the prototype of another one, would it be better to separate taskOperations as a standalone object and using Object.create(taskOperations) like I did below to avoid clogging the memory with the same functions for all the objects created?

Would this be the most optimized way or is using classes for example more effective for this?

const taskOperations = {
    getTitle() {
        return this.title;
    },
    setTitle(title) {
        this.title = title;
    },
    getDateCreated() {
        return this.dateCreated;
    },
    setDateDue(dateDue) {
        this.dateDue = dateDue;
    },
    getDateDue() {
        return this.dateDue;
    },
    setPriority(priority) {
        this.priority = priority;
    },
    getPriority() {
        return this.priority;
    },
};

// Task factory
const createTask = (title) => {
    let task = Object.create(taskOperations);
    task.title= title;
    task.dateCreated= new Date();
    task.dateDue= "";
    task.priority= false;
    task.note= "";
    task.done= false;
    task.projectID= 0;
    return task
};
1

There are 1 answers

1
ggordon On BEST ANSWER

Proposal

With your current concern being replicating multiple similar functions on different objects as this is increasing memory usage.

We could take advantage of the type of language being used i.e. interpreted and little room for "real" data encapsulation.

In Javascript, all is "public" or accessible i.e. no data encapsulation and so the object properties are sufficient to access/change the state of the object (i.e. for where you have setters/getters eg setTitle and getTitle).

For more complex operations and in an attempt to reduce the memory overhead of multiple similar functions you could create a "singleton" service ( I used quotations here as again this is more or less how you apply it and not hope to restrict it because of the type of language you are using) class that has the methods or intended operations. Each method would then accept a task object and any other details required to perform the operation and update the task's respective state if necessary. This would mean that for 15,000 tasks in memory, you would no longer need 15000 * [num of task operations].

This could look like (again this implementation is really syntactic sugar and decision based):

const TaskService = {
    getTitle(task) {
        return task.title;
    },
    setTitle(task,title) {
        task.title = title;
      
    },
    getDateCreated(task) {
        return task.dateCreated;
    },
    setDateDue(task,dateDue) {
        task.dateDue = dateDue;
    },
    getDateDue(task) {
        return task.dateDue;
    },
    setPriority(task,priority) {
        task.priority = priority;
    },
    getPriority(task) {
        return task.priority;
    },
};

Whether classes or not, the idea is to separate concerns where possible i.e. keeping the class simple, we could have classes just to store state (eg where you created the object in the factory) and classes that allow us to perform different actions.

I hope this helps the decision process. There are many software design and anti-design patterns.

Further Reading

A lot of literature on design patterns usually reference typed languages (eg, Java, C# etc) but I hope this may also be helpful

Article - https://www.toptal.com/javascript/comprehensive-guide-javascript-design-patterns Additional Ways To Create Javascript objects - Which way is best for creating an object in JavaScript? Is `var` necessary before an object property?