Is it possible to retrieve the timestamp for each call in a callstack?

I'm interested in utilizing the callstack of a function call to create an accurate timeline when an exception is thrown. While I know that I can retrieve the name, caller name, line number, and file for each call from the callstack, I am wondering if it's possible to also obtain the timestamp for each called function.


    function callA() {
        callB();
    }

    function callB() {
        callC();
    }

    function callC() {
        throw new Error('Boom');
    }

When checking the chrome console, it shows:

Uncaught Error: Boom
    at callC (<anonymous>:13:8)
    at callB (<anonymous>:8:2)
    at callA (<anonymous>:3:2)
    at <anonymous>:1:1

What I envision working with in my JavaScript code would be something like this:

Uncaught Error: Boom
    at callC (<anonymous>:13:8) timestamp : 1559117311448 
    at callB (<anonymous>:8:2) timestamp : 1559117311449
    at callA (<anonymous>:3:2) timestamp : 1559117311449
    at <anonymous>:1:1

Answer №1

Math.ceil(Date.now() / 1000) calculates the number of seconds that have passed since Jan 1, 1970. Alternatively, you can use the performance.now() method for a more precise time duration between two points in your code.

function callA() {
  console.log(performance.now());
  callB();
  console.log(performance.now());
}

function callB() {
  let startTime = performance.now();
  callC();
  let endTime = performance.now();
  console.log(`Start Time: ${startTime} - End Time: ${endTime}`);
}

function callC() {
  throw new Error('Unexpected error occurred');
  console.log(Math.floor(Date.now() / 1000))
}

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 causes certain event handlers to be activated when using dispatchEvent, while others remain inactive?

When it comes to event-based JS, there are two main APIs to consider: event listeners and event handlers. Event listeners can be registered using addEventListener, while event handlers are typically registered with an API similar to target.onfoobar = (ev) ...

Retrieving a component from a webpage with Cypress

https://i.sstatic.net/ZvlGU.png I am trying to extract just the order ID from the href attribute using Cypress. Can anyone assist me with this? Below is my Cypress code snippet: `cy.get('[href="/practice/orders"]').click() cy.get(& ...

Browser Compatibility with AngularJS

Encountering an issue with AngularJS compatibility - are there any features not supported by Google Chrome? We have developed the AngularUI Calendar and utilized the JSFiddle link below: http://jsfiddle.net/joshkurz/xqjtw/52/ The UI calendar works on Fi ...

Hear the alteration of the JavaScript variable

Suppose there exists a code snippet at point 2 var point2IsReady = true; At point 1, I am tasked with implementing the following logic: Once the value of point2IsReady is changed (to true), then display an alert saying 'ready!'. Considerations: ...

Can you assign a particular symbol to a function for naming variables?

When incorporating a new function into jQuery: $.fn.boxLoader = function() { var FilterSelect = ($("[id^='filter_a_selector_']")); // perform actions on FilterSelect return this; }); Then, when a par ...

Is the memory efficiency of Object.keys().forEach() in JavaScript lower compared to a basic for...in loop?

Picture a scenario where you have an extremely large JS object filled with millions of key/value pairs, and your task is to loop through each of them. Check out this jsPerf example that demonstrates the different techniques for accomplishing this, highlig ...

JavaScript object value not accessible due to SyntaxError: Unexpected token, expected ,

While working on aliases.js, I encountered an issue when attempting to retrieve the value of the 'SELECT_HOST' property from the imported actionTypes object. This led to a "SyntaxError: Unexpected token, expected ," error according to Webpack. I& ...

"Can you help me understand how to establish a character limit for the MUI autocomplete

Hey everyone, I have successfully created an auto-complete feature using Material UI and an API. Now, I am facing a challenge where I need to limit the suggestions returned by the autocomplete to only show matches when the user types at least 3 letters. Ca ...

Having trouble with less.modifyVars not functioning properly in Visual Studio?

I attempted to dynamically change the LESS variable using HTML within an AngularJS function. The code worked fine on XAMPP with simple HTML and CSS, but when I transferred it to an enterprise app in Visual Studio, it stopped functioning properly. While the ...

Using JavaScript, pass a single parameter to a function that accepts multiple arguments

Within my service, the Angular function is defined as follows: $delegate.isConfigurable = function (product, config) { if (product) { ///..... } return config.getDetail(); }; ...

Modifying the values of Highcharts-Vue chart options does not result in any changes once they have been

I recently started incorporating Highcharts into my Vue project using the highcharts-vue library. A) Initially, in pure JavaScript, the following code snippet functions as expected: let b = 5; let data = { a: b } console.log('data', data.a) ...

Is there a way to pause and await the completion of an axios post request within a different axios interceptor?

Here are my axios interceptors: instance.interceptors.request.use( (config) => { const accessToken = localStorage.getItem("access_token"); const auth = jwt_decode(accessToken); const expireTime = auth.exp * 1000; co ...

Changing between two images using HTML and CSS

I am currently designing a WordPress theme and I would like to create an effect where two different thumbnail images switch on hover. The code that I have come up with so far looks something like this : <a class="thumb" href="posturl"> <img src= ...

The jQuery plugin is not functioning properly on the subdomain

Encountered an issue where a jQuery plugin is not loading in Chromium for a peculiar reason. Interestingly, the plugin loads fine when accessing the page through the root domain but fails to load when accessed via a subdomain in Chromium. However, everyth ...

Locate the row just before the last one in the table

I have a table that dynamically creates rows, and I need to locate the second to last row in the table. HTML Code <table class="table"> <tr><td>1</td></tr> <tr><td>2</td>< ...

Integrating new information into an existing JSON file using React Native

Currently, I am attempting to input new data into an existing JSON file using text input. In my project using React Native, I have a JSON file named PartyInfo.json where certain data is stored. The goal is to have this data passed from a form and saved in ...

Ensure that users must confirm their actions through a message prompt when attempting to exit the website

Looking to add a confirmation box that pops up when someone tries to leave my website. It's important to prevent any information from being lost if they accidentally navigate away. Can you provide detailed instructions on where exactly I should place ...

Enclose an image with a <div> element while maintaining the <div> height at

Encountering a challenge where positioning an element on top of an image at specific coordinates (20% from the top and 30% from the left) is required. Initially, the solution involved wrapping the image in a div with a relative position and using absolute ...

Error encountered while attempting to render a form within a partial in Rails 5: "simple_fields_for" method is not defined for the SimpleForm::FormBuilder instance

This is a continuation from this thread: Passing a form as a local to a ajax rendered partial in Rails 5 I've searched extensively but haven't been able to find a working solution. Relevant Controller (profits_controller.rb): def new_tabs ...

Troubleshooting the defects in the string-to-json module functionality

I am dealing with string data var str2json = require('string-to-json'); var information={ "GTIN" : "GTIN 3", "Target Market" : "Target Market 3", "Global Location Provider Name(GLN) 3" : "Global Locati ...