Remove the original object in AngularJS after making a complete copy to a new object

My application loads a large JSON file using a <script> tag. The JS file containing the JSON is around 280k in size and follows a standard JS definition:

var _countries = {"country:{"USA":{...},"GBR":{ ...},"FRA":{ ...}, etc, etc ...}}} ;

Upon launching the app, the JSON is loaded into memory and then I aim to copy it to a new $rootScope variable rather than just creating a reference to the original object. Once the copying is complete, I intend to delete the original _countries object.

$rootScope._countries= angular.copy(_countries) ;
_countries = null ;

However, I am unsure how to determine when the object has been fully copied before deleting the original _countries object. With JavaScript's asynchronous nature, I want to avoid the risk of $rootScope._countries not containing the full object prior to nullifying the original one.

Additionally, I'm curious if there is a more efficient way to copy the original object without relying on angular.copy().

Answer №1

In Summary

  • Don't stress about waiting for a simple JSON assignment.
  • No need to remove variables in JS to optimize memory usage.

Synchronous Execution

A basic data variable assignment like this will always run synchronously:

const foo = {...superBig} // definitely just JSON

If you're reading from the JSON within the variable assignment, such as...

var _countries = funcToLoadJSON('path-to-my-json')

...the execution may vary depending on how the read operation is implemented - some methods are synchronous while others are asynchronous. Check out some examples.

Memory Management

Deleting objects in Javascript isn't straightforward. JS engines handle garbage collection based on various factors behind the scenes.

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 steps should I take to incorporate Google sign-in on my website and gather user information efficiently?

I am looking to add the Google sign-in button to my website. While I am knowledgeable in HTML, PHP and JavaScript are not my strong suits. My goal is to allow users to sign in with their Google account and securely store their information on my database th ...

Error: Jersey and Maven are unable to locate the MessageBodyWriter for media type=application/json

I am currently facing an issue with my webservice in Netbeans. Everything works perfectly within the IDE, but when I try to run the jar file using the command "java -jar FILENAME PARAMETERS," I encounter the following error: MessageBodyWriter not found fo ...

Frequent occurrences of Android ConnectTimeoutException are being observed

I am facing an issue with my mobile application where all the webservices were working perfectly until Friday night. However, now every JSON I call is throwing a ConnectTimeoutException. These JSON services were functioning correctly last week and there ha ...

A guide on serializing and deserializing Java objects using javax.JSON

Ever since the release of Java 7, a JSON standard has been in place. Is there an easily accessible or widely accepted method for converting Java objects to JSON and then back again? ...

Unable to cancel the setTimeout function by using clearTimeout as the value appears to be null for unknown reasons

Within my react-native application, I am attempting to halt the execution of a setTimeout function by utilizing clearTimeout. The instance of setTimeout is stored in a global variable. let timeoutId: any = null; const doOtp = () => { if(can ...

Convenient way for users to easily choose an icon from a vast selection of icons

I am currently working on a web application that allows users to create new categories. These categories are then inserted into a database using form post. Each category should have an icon associated with it, and I want users to be able to select the ico ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

What are the steps to integrating a repository into the clean architecture design pattern?

I have been following Uncle Bob's clean architecture principles in developing my medical application's API. However, I am facing some challenges in determining where certain components should be implemented. Within my application layer, I have a ...

Disconnect event happening

I am currently studying nodejs using a French tutorial on creating a chat application with nodejs https://www.youtube.com/watch?v=8jkkd2Ohte8 However, I am facing an issue when trying to track the users who leave the chat room. This is my Server.js code: ...

Acquiring data from a JavaScript file in JSON format with JINT

Recently, I received a JavaScript file that contains two JSON objects. The first object stores different languages, while the second object contains various labels. var languages = {"Languages":["English","Cymraeg","Deutsch"]}; var labels = [{"$JOB":["Job ...

Performing addition in Angular 2 with the help of TypeScript

Here is a code snippet of a component I have written: export class AppComponent { public num1: number = 2; public num2: number = 3; public sum: number = 0; public add() { this.sum = this.num1 + this.num2; } } However, when I r ...

The delay function in jquery is not functioning as expected

Hey everyone, I'm having trouble with my timing function in the code below... $( document ).ready(function() { $.ajax({ type: 'GET', url: 'conversation.json', dataType: 'json', success ...

What is the best way to transfer an object between views in Django?

Background/Issue In my web application, I have a job that involves running a python script to log into LinkedIn. This script launches headless chromium, navigates to the LinkedIn login page, and logs in with the proper credentials. However, sometimes Link ...

The styled-components seem to be having trouble injecting the necessary CSS to handle all the various props

I am curious to understand the potential reasons for styled-components not injecting all the necessary CSS into a page's header. In an existing project, I have defined a simple button like this: const Button = styled.button` background-color: ...

Searching for one specific document in FireStore

Context: I'm facing an issue where I am attempting to retrieve a single document from a collection in Firestore using a value rather than its ID, essentially implementing a login system. I have attempted the following approach based on this solution ...

Error Thrown While Attempting to Capture Retrofit JSON Response

I am delving into logging Retrofit responses for the first time and grasping that OKHttp is automatically bundled in Retrofit 2.x releases. After crafting my code, I encountered the following error: Error Message: java.lang.NoSuchMethodError: No virtu ...

Determine the initial left position of a div element in CSS prior to applying

Scenario : const display = document.querySelector('.display'); const container = document.querySelector('.outer-div'); document.addEventListener("click", (event) => { if (!event.target.closest("button")) return; if(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, } ...

Java: techniques for parsing this JSON

In my quest to extract information from a JSON object, I am seeking the values of either tel or user_id. Take for instance this JSON data: { "status":"ok", "account":{ "0":{ "user_id":"2", "thirdparty_id":"200", "tel":"28 ...

What's in Meteor 1.3? Discover the best practices for declaring your helpers!

As I navigate through Meteor 1.3's include logic, I find myself facing a challenge. In an app that I am currently piecing together, my /client/main.js file contains: import '../imports/startup/accounts-config.js'; import '../imports/u ...