Tips for Optimizing Typescript Queries to SQL Server

In my C# application, I have the following function:

foreach (var p in proglist)
{
  programData.GetData1= new List<GetData1_ViewModel>(GetData1(programid, reportingdate));
  programData.GetData2= new List<GetData2_ViewModel>(GetData2(programid, reportingdate));
  programData.GetData3= new List<GetData3_ViewModel>(GetData3(programid, reportingdate));
  programData.GetData4= new List<GetData4_ViewModel>(GetData4(programid, reportingdate));
  programData.GetData5= new List<GetData5_ViewModel>(GetData5(programid, reportingdate));
}

Similarly, in my Typescript application, I have the matching function:

for (const p of proglist) {
  this.data1 = [];
  this.data2 = [];
  this.data3 = [];
  this.data4 = [];
  this.data5 = [];
  await Promise.all([
    (this.data1 = await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData1")),
    (this.data2 = await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData2")),
    (this.data3 = await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData3")),
    (this.data4 = await this.GetData4(p.programId, reportingdate, "GetData4")),
    (this.data5 = await this.GetData5(p.programId, reportingdate, "GetData5")),
  ]);
}

Both functions interact with the same database tables and stored procedures.

Surprisingly, the C# code completes in 5 seconds, while the Typescript code takes 189 seconds.

I am puzzled by the significant speed difference between the two. What optimizations can I implement to improve the performance of the Typescript function?

Answer №1

Suppose this.GetMeasureForScorecard() as well as this.GetData5() each return a Promise that, once fulfilled, will hold the requested value. In that case, there is no need to use await within Promise.all() because doing so would go against its purpose, which is to resolve unfulfilled promises (not yet awaited).

for (const p of proglist) {
  const [data1, data2, data3, data4, data5] = await Promise.all([
    this.GetMeasureForScorecard(p.programId, reportingdate, "GetData1"),
    this.GetMeasureForScorecard(p.programId, reportingdate, "GetData2"),
    this.GetMeasureForScorecard(p.programId, reportingdate, "GetData3"),
    this.GetData4(p.programId, reportingdate, "GetData4"),
    this.GetData5(p.programId, reportingdate, "GetData5"),
  ]);
}

Executing multiple function calls with the await keyword will make the Javascript interpreter process them sequentially rather than concurrently (or semi-concurrently), which may not align with your intended outcome (I presume).

await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData1");
// Awaits resolution of the above Promise
await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData2"),
// Awaits resolution of the above Promise
await this.GetMeasureForScorecard(p.programId, reportingdate, "GetData3"),

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

Is it recommended for Protractor Page Objects to reveal ElementFinder?

Our team is currently debating whether or not to prohibit the exposure of "ElementFinder" and "ElementArrayFinder" in our Page Objects. The main point of contention is a quote by Simon Stewart. Page Objects Done Right - selenium conference 2014 (page.7) ...

What programming language is the best choice for Angular 2 development?

As someone who is new to Angular 2, I've discovered that developers have the option to use TypeScript, ES6, and ES5 for their development needs. I understand that TypeScript is considered the superset of ES6 and ES5. Given the stark differences in sy ...

What is the best way to initiate a dialog within the handleSubmit function?

In my project, I have a component called SimpleDialog which is defined in the File.tsx file. export default function SimpleDialog() { const handleSubmit = (event: any) => { <SimpleDialog />; } return( <form> <Button type="submit& ...

Is it possible to include the term 'public' exclusively within a .ts file in a TypeScript TSLint React environment?

I'm struggling to understand why I am encountering an error in VSCode while working on a react typescript project with tslint setup. The error message states: 'public' can only be used in a .ts file. [Also, I'm wondering why I' ...

Defined a data type using Typescript, however, the underlying Javascript code is operating with an incorrect data type

Recently delving into Typescript and following along with an educational video. Encountered a strange behavior that seems like a bug. For instance: const json = '{"x": 10, "y":10}'; const coordinates: { x: number; y: number } = JSON.parse(json); ...

Loading React.js components before data fetch is complete

My app is encountering an issue where it renders before the fetch operation is completed, resulting in incorrect rendering. Below is the code snippet: componentWillMount() { fetch('http://localhost:8081/milltime/login?users='+this.state.e ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

Looking to retrieve the text content of an element using jQuery?

My goal is to use jQuery mobile to transfer a user to a linked page when they click on an <li> element, and then display an alert with the text of the list element they clicked. However, my current implementation only shows an empty alert box. < ...

Issues with error handling in ExpressJS arise frequently

In the server.js file, towards the very end, there is a block of code that appears to handle errors: app.use(logErrors); function logErrors (err: Error, req: Request, res: Response, next: NextFunction) { console.log(err); mongoDal ...

Arranging serialized information from a tabular format and retrieving specific data

: I'm currently attempting to utilize an AJAX POST request to send data from a dynamic webpage that includes information gathered from a table form, a date selection box, and comment text input. The webpage is generated as a PHP file on a GET request ...

Assign individual heights to multiple iFrames according to their content's height

Dealing with multiple iframes on a single page. Each iframe is sourced from my domain. The goal is to automatically calculate and set the height of each iframe on the page. The current code sets all iframe heights to match that of a specific iframe: fun ...

How can one effectively manage irregularly nested object/arrays within a Meteor framework?

Having difficulty finding a smart and effective method to manage nested arrays/objects with varying dimensions in nodeJS. These irregular arrays/objects can have 1, 2, 3, or more dimensions. For instance, consider these 2 different scenarios : Scenario 1 ...

Tips on showcasing a JSON object containing two arrays in HTML using a pair of for loops

I am facing an issue with displaying data from two tables in my PHP script. The personal information is being displayed correctly, but the books related to each person are not showing up. I suspect that my approach might not be efficient enough for handlin ...

Using Angular to fetch API response and convert it into a promise

I've been following the Angular tutorial available at https://angular.io/tutorial/toh-pt6. The tutorial demonstrates how to retrieve a Json response from an API call and then match it to a promise. One specific example from the tutorial is as follows ...

There is no response from Ajax

I am facing an issue with my AJAX call in communication with my Rails controller. Even though the AJAX call itself seems fine, the callback function does not contain any data. Here is the AJAX request I'm making: $.ajax({ url: '/get_progres ...

When using `res.send()`, an error of Type Error may be encountered stating that the callback is not a function. However, despite this error,

I am currently working on a function for my Node.js server, using Express as the framework and MongoDB as the database. This function involves calling two mongoose queries: (1) Retrieving filtered data from one collection (2) Aggregating data from anothe ...

The Express router is failing to recognize the mongoose model

Currently, I am developing a node.js application and encountering an issue where I receive a reference error stating that Post is not defined every time I run this specific code. Interestingly, when I move the post route from submit.js to app.js, the code ...

Handling TextChanged Event of a TextBox in ASP.NET using C#

I'm currently designing a POS screen that allows barcode scanning directly into a textbox. I want to implement a code behind procedure that adds the barcode-related data to the grid as soon as the textbox text changes. This is how my textbox looks: &l ...

Highlight react-bootstrap NavItems with a underline on scroll in React

I am currently working on a website where I have implemented a react-bootstrap navbar with several Nav items. My goal is to enable smooth scrolling through the page, where each section corresponds to an underlined NavItem in the navbar or when clicked, aut ...

Encountering an error of ExpressionChangedAfterItHasBeenCheckedError while trying to refresh the

I'm encountering an issue that I need help with: https://i.stack.imgur.com/4M54x.png whenever I attempt to update the view using *ngIf to toggle on an icon display. This is what my .ts file looks like: @Component({ selector: 'app-orders&ap ...