Optimal utilization of JSON in JavaScript API: Enhancing Performance, Reinforcing Maintainability, and Optimizing Resources

Currently, I am working on developing an application using Laravel and VueJS (along with Vuex). Although I do not have much experience in working with these frameworks or front-ends, I am curious to know the best practices for utilizing the data received from the API on the front-end.

My application involves accounts that can contain one or more users. The frontend displays an overview of all accounts and also a list of users for administrative purposes. The API response example is structured as follows:

To provide some context:

// When fetching account(s)
data: {
  account: {
    id: "a1",
    name: "Account1",
  },
  accounts: [
    acount x,
    account y,
    ...
  ]
}  
// When fetching user(s)
data: {
  user: {
    id: "u1",
    name: "User1"
  },
  users: [
     ...
  ]
}

For optimal performance, it seems efficient to directly store the JSON objects received from the API in the Vuex store. These stored objects will then be used to populate the update views and overview tables.

From a software engineering perspective, I prefer creating ES6 class objects. This approach offers several advantages:

  • I can implement functions like save(), update(), or fetchUsers() for both user and account objects, which can interact with fetch or update functions in the Vuex FLUX store.
  • Inheritance can be utilized for functions such as save().
  • If needed, additional functions can be implemented within the object class.
  • There may be other benefits specific to modeling that I am yet to uncover.

In this scenario, I would convert the JSON data into ES6 class objects and store these objects or lists of objects in my store(s) instead of storing the raw JSON objects.

What I'm seeking guidance on:

  • Are there any established conventions (particularly in ES6) that should be followed, or what is considered the best practice in this situation? Which option out of the two mentioned (or any other) is recommended?
  • What impact does creating these objects have on performance or resource usage, especially on mobile devices?
  • Is the potential performance or resource impact negligible? Is there a recommended limit on the number of objects that should be created?

I believe the details of the situation and my questions are clear, but feel free to request additional information if needed.

Answer №1

It is often recommended to maintain a flat structure without nested objects in state management stores like Vuex.

  • Entries should be normalized using object IDs

Including full ES6 classes may not be the most suitable approach.

For further insights, consider exploring talks and blogs on the topic:

Vuex Best Practices (linusborg)
Vuex State Normalization
Vuex Best Practices for Complex Objects

If you are relatively new to these frameworks, it might be worth considering if Vuex is truly necessary for your specific situation.

As stated in the reference documentation below, there is a tendency to overuse such tools by storing everything while learning. The general consensus leans towards utilizing Vuex only when essential (particularly when multiple components need access to or update the data).

It's important to assess whether this applies to your application based on the information provided.

More details can be found here:

What Data Should Be Stored in Vuex

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

Contrasting onevent with addEventListener

After studying various DOM events, I attempted to implement the 'blur' event on the HTML body. My first attempt was with onblur document.body.onblur = () => { dosomething(); } and I also tried using AddEventListener document.body.addEven ...

Transforming data with D3.js into a string representation

Recently, I stumbled upon a function called "stringify" that seems like a fantastic tool for converting flat data into a Json format. If this function lives up to its potential, it could potentially save me countless hours of writing recursive code in ASP ...

The appropriate method for transferring a prototype to an object

From my perspective, prototypes work like this: let Animal = function() { this.bark = "woof"; } Animal.prototype.barkLoud = function() { return this.bark.toUpperCase(); } let x = new Animal(); x.barkLoud() = "WOOF"; I f ...

Using Node.js and Express to import a simple JavaScript file as a router

Can anyone help me understand how to import the user.json json file into my user.js? I want the json file to be displayed when typing /user but I'm struggling with the new version of Node. index.js import express from 'express'; import body ...

Developing an easily optimized library using rollup to remove unnecessary code branches

I'm currently in the process of developing a component library using rollup and Vue with the goal of making it tree shakable for others who import it. The configuration setup is outlined below: Here's a snippet from package.json { "name": "re ...

Can you explain the distinction between object allocation using the '=&' operator and the Object.create() method?

I have been delving deep into JavaScript object operations. My question revolves around the difference between const me = Object.create(person); and const me = person;. Both of these operations provide a similar output as they reference the object to a new ...

Generating images with HTML canvas only occurs once before stopping

I successfully implemented an image generation button using Nextjs and the HTML canvas element. The functionality works almost flawlessly - when a user clicks the "Generate Image" button, it creates an image containing smaller images with labels underneath ...

Tips for configuring CakePHP to trigger the second submit button when the enter key is pressed

My form includes two submit buttons: "cancel" and "find." While both buttons work correctly when clicked, pressing the enter key always triggers the submission of "cancel." I don't want to change the button order in the form. To address this issue, I ...

Tips for aligning the dropdown menu to start from the border of the menu bar instead of displaying directly below the text

I have designed a menu-header section for my website, inspired by this image. I created a fiddle to showcase it. However, I am facing an issue where the dropdown elements for "PROGRAMS" and "WORLD OF NORTHMAN" should start from the border of the header ins ...

Finding the element in the HTML using selenium and Python

Recently, I have been working on automated testing using Selenium. However, I have encountered a strange issue where I am unable to locate the element. Can someone please provide me with guidance on how to handle this situation? driver.find_element_by_xpa ...

Getting field values from Firestore in a React application

Within my firestore document, I have three fields that store boolean values critical for subsequent processing. To access these boolean values in my program, I need to figure out how to read the fields of a document. This process should be straightforward, ...

What is the method for invoking an imported function in a template?

Let's analyze the code snippet below, which utilizes a method and an imported function: <template> <div> {{sayHello()}} {{sayDate()}} {{DateTime.now()}} <div> </template> <script> import {DateTime} from & ...

Enhancing AngularJS functionality through the integration of jQuery within a TypeScript module

As I try to integrate TypeScript into my codebase, a challenge arises. It seems that when loading jQuery and AngularJS in sequence, AngularJS can inherit functionalities from jQuery. However, when locally importing them in a module, AngularJS fails to exte ...

Synchronize data with Microsoft AJAX using force sync

When working with the Microsoft AJAX framework, I encounter a problem where my AJAX calls are asynchronous when I actually need them to be synchronous. I'm struggling to find a solution for this issue. In addition, I have been having difficulty findi ...

The Cross-Origin Request has been blocked due to the Same Origin Policy prohibiting access to the remote resource. The reason for this is that the CORS preflight response was unsuccessful

SERVERSIDE // Establishing Headers app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); res.header("Access-Control-Allow-Headers ...

Could you provide insight into the reason behind debounce being used for this specific binding?

function debounce(fn, delay) { var timer return function () { var context = this var args = arguments clearTimeout(timer) timer = setTimeout(function () { fn.apply(context, args) }, delay) ...

Tips to stop automatic scrolling when tapping on a tab

Currently, I am encountering an issue with the tabs in my project. Whenever I click on a tab, the page scrolls down and I can't seem to figure out why. Below is the snippet of my HTML code: <ul class="list"> <li class="tab1 active"> ...

"Encountering an Error with Route.get() when attempting to utilize an imported

I have a function that I exported in index.js and I want to use it in test.js. However, when I try to run node test, I encounter the following error message: Error: Route.get() requires a callback function but got a [object Undefined] What am I doing wro ...

What could be causing the error 'i is not defined' in my Vue.js component script when using a basic for loop?

I have a task where I need to sort an array by version and then move all elements starting with 'ipad' to the end of the list. This code snippet is extracted from a single file Vue.js component: computed: { orderedUsers: function () { ...

What steps should I take to resolve the "You do not have authorization to access this directory or page" error in Azure App Services?

Currently, I am attempting to deploy my Next.js 13 application to AppServices using Github Actions. The compilation process on Github was successful, however, I am encountering an issue where my application does not seem to be deploying or starting up prop ...