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

Tips for including a footer element in React:How can a footer division be

I need help with implementing a footer in my React web app. Currently, the body section in index.html looks like this: <body> <div id="root"></div> <script src="index.js"></script> </body> Inside index.js, the rend ...

An issue occurred while trying to retrieve the js file, as an error with code net::ERR_ABORTED 404 (Not

Although this question has been asked before, I am unsure where to find the solution. My express backend server needs to render an HTML page with main.js in it upon launch. app.js code: var createError = require('http-errors'); var express = req ...

I did not anticipate the React setState function to behave in the manner it did

While working on form validation for my page, I came across a tutorial showcasing a validation method that seems to be functioning correctly. However, there is one aspect that I am struggling to grasp. The tutorial suggests declaring a variable before the ...

Unable to scroll to the top of the page with JavaScript

I tried the code below but it didn't quite do the trick. Can someone assist me with refreshing the page to the top every time it loads? window.addEventListener('load', function(){ window.scrollTo(0,0) }) window.onload = (event) => { ...

Is there a way to ensure that a "catch all other" route in the Vue Router will also capture the URL if a portion of it matches a predefined route?

After following the suggestion to implement a catch all route from this article, I realized that it does not capture URLs that partially match a defined route. routes: [ { path: "/album/:album", name: "album", component: Album, } ...

Using checkboxes in an Express application

Currently, I am working on the task of parsing checkbox values into an array using express/ejs. Users are required to fill out a form and select checkboxes as shown below: Answer: Checkbox: The goal is to create two arrays from the input data: answer = ...

The npm error message states: "Unable to locate the 'readable-stream' module."

Recently, I installed node js on my Windows 10 machine with version v4.4.2. However, whenever I attempt to run npm install or check npm version, it throws the following error message. Any assistance on this matter would be highly appreciated. Error: Canno ...

What is the best way to incorporate one Div within another Div using JavaScript?

I have a single main div with an id, and I am looking to insert 4 additional child divs inside it. Each of these child divs will then contain 5 more divs each. The code snippet that I currently have is as follows: $( document ).ready(function() { for( ...

Convert a string to HTML using AngularJs

Snippet: <tr ng-repeat="x in orderCsList"> <td class="ctn"><input type="checkbox" ng-model="x.checked"></td> <td class="ctn">{{ x.wdate }}</td> <td class="text-left">{{ x.wid }}</td> <td class="te ...

Ways to modify color while user moves the screen

I'm working with some jQuery code that changes the colors of elements as the user scrolls down, and I want it to revert back to the original color when scrolling back up. However, the script is not behaving as expected... Here is the original working ...

Transform SVG with a Click

Can someone assist me with rotating the pink area on click and moving it to a new angle from its current position? I am looking for a way to save the current position of the pink area and then, when clicked, move it smoothly to a new position. Your help ...

Angular HTTP requests are failing to function properly, although they are successful when made through Postman

I am attempting to send an HTTP GET request using the specified URL: private materialsAPI='https://localhost:5001/api/material'; setPrice(id: any, price: any): Observable<any> { const url = `${this.materialsURL}/${id}/price/${price}`; ...

Making an Ajax request by leveraging the power of an image tag

I am facing a challenge with trying to establish communication on my server between port 80 and port 8080 through an ajax request. I understand the implications of CORS and the cross domain request origin policy, as well as the potential solution involving ...

Optimal Method for Organizing Items in Express.js Using Mongodb

Can you share your insights on the best practices for sorting objects in expressjs + mongodb(mongoose) web applications? Imagine we have a Product model with four attributes: var ProductSchema = new mongoose.Schema({ name: String, // "Summer T-sh ...

What steps can be taken to enhance the efficiency of this complex nested asynchronous loop?

The issue at hand involves an array of objects structured like this: let myObj = [ {'db1':['doc1','doc2','doc3']}, {'db2':['doc4','doc5']}, {'db3':['doc7','doc8 ...

Creating an executable JAR for your Next.js application: A step-by-step guide

Is there a way to ensure that my Next.js file can run seamlessly on any computer without the need for reinstallation of all modules? In my folder, nextjs-node, I have the following subfolders: components lib public node_modules page style package.json I ...

Incorporating HTML themes within ReactJS

While I am still relatively new to ReactJS, I am eager to expand my understanding of it with this question. I have a preexisting HTML/CSS theme that I would like to integrate into a React application. Is there a method to incorporate this theme seamlessly ...

Implementing specific CSS styles for images that exceed a certain size limit

Currently, I am facing a dilemma as I work on developing a basic iPhone website that serves as a port for my blog. The main issue I am encountering is the need to add a border around images above a specific size and center them in order for the blog to hav ...

Is it possible to toggle all parent targets in Bootstrap?

When trying to showcase my point, I believe it is best demonstrated by visiting Bootstrap documentation at https://getbootstrap.com/docs/4.0/components/collapse/ and viewing the "multiple targets section." In this section, you will find three buttons: togg ...

Exploring the contrast between 'completed' and 'upcoming' in callback functions within node.js

Within the passport documentation for authentication configuration, there is a function that appears rather intimidating as it utilizes the enigmatic function "done." passport.use(new LocalStrategy( function(username, password, done) { User.findOne( ...