Js-Performance-Debugging js8
Js-Performance-Debugging js8
Topperworld.in
❖ Performance
• You need to be aware that this approach involves several iterations while
jQuery loops through each of DOM elements and tries to find a match.
©Topperworld
JavaScript
The above code makes use of JQuery to manipulate the DOM which is not
the best option as instead of doing it this way we can make use of the
getElementById method which the document object provides us.
• Where we load our style sheets and JavaScript files can have an effect
on the perceived speed of a page if the user can see some content even
before the JavaScript takes in it’s a much better experience when a
browser encounters a script tag it stops what it’s doing completely to
download and run the script if we put our scripts at the top of the page
that means it’s downloading our JavaScript files first and then later
getting down to parse our HTML body this means while our scripts are
downloading there is no content for the user to see now if we put our
scripts at the bottom of the page instead by the time, our script starts
loading there’s at least some content on the page making the page seem
to have loaded faster.
©Topperworld
JavaScript
• The defer attribute specifies that the script should be executed after the
page has finished parsing, but it only works for external scripts.
Example:
// using defer:
<script type="text/javascript" src="path/to/script2.js"
defer></script>
• When your code base gets bigger a switch statement is usually more
efficient than a set of nested ifs.
©Topperworld
JavaScript
Example:
// push() method
let Animals = ["Tiger", "Giraffe", "Horse", "Deer"];
Animals.push("Zebra");
console.log(Animals);
Output:
• Bundling your application’s components into *.js files and passing them
through a JavaScript minification tool will make your code cleaner.
• There are tons of code minification tools that are available for free.
©Topperworld
JavaScript
• You can get the most out of the scope variable (this) by rewiring it using
the special call() and apply() methods that are built into each function.
❖ Debugging in JavaScript
It is common to have errors while writing codes and the errors can be
due to syntax or logic.
There can also be errors in the code which can remain invisible to the
programmer’s eye and can create havoc.
• The debugger keyword is used in the code to force stop the execution of
the code at a breaking point and calls the debugging function.
©Topperworld
JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<title>Debugging in Javascript</title>
</head>
<body>
<h4>
Debugging demonstrations using Debugger keyword
</h4>
The solution of 20 * 5 is:
<p id="test"></p>
<p>
If the debugger is turned on the code stops
execution at the start of the debugger
</p>
<script>
const x = 20;
const y = 5;
const z = x * y;
debugger;
document.getElementById("test").innerHTML = z;
</script>
</body>
</html>
©Topperworld
JavaScript
Output:
✓ Previously implementing debuggers was tough but with time and with
the advent of modern browsers, various built-in debuggers came into
implementation.
✓ One can resume the flow control after the examination is done.
✓ This all can be done through the “Console” of the debugger menu.
©Topperworld
JavaScript
• In this method, Breakpoints are set in code which stops the execution of
code at that point so that the values of variables can be examined at that
time.
Here are some advantages of using Breakpoints over the console.log() method:
©Topperworld
JavaScript
following image, a breakpoint is set at line 23. (The code used is the same
as the above example)
• These errors can occur due to a fault from the programmer’s side or the
input is wrong or even if there is a problem with the logic of the program.
But all errors can be solved and to do so we use five statements that will now
be explained.
✓ The try statement lets you test a block of code to check for errors.
✓ The catch statement lets you handle the error if any are present.
✓ The throw statement lets you make your own errors.
©Topperworld
JavaScript
✓ The finally statement lets you execute code after try and catch.
The finally block runs regardless of the result of the try-catch block.
Example 1:
try {
dadalert("Welcome Fellow ");
}
catch (err) {
console.log(err);
}
Output: In the above code, we make use of ‘dadalert’ which is not a reserved
keyword and is neither defined hence we get the error.
©Topperworld
JavaScript
Example 2:
function Func() {
let a = 10;
try {
console.log("Value of variable a is : " + a);
}
catch (e) {
console.log("Error: " + e.description);
}
}
Func();
Output: In the above code, our catch block will not run as there’s no error in
the above code and hence we get the output ‘Value of variable a is: 10’.
©Topperworld
JavaScript
• The catch statement allows you to display the error if any are found in
the try block.
try {
Try Block to check for errors.
}
catch(err) {
Catch Block to display errors.
}
• The throw statement lets you create your own custom error.
• By using throw together with try and catch, you can easily control the
program flow and generate custom error messages.
©Topperworld
JavaScript
Example:
try {
throw new Error('Yeah... Sorry');
}
catch (e) {
console.log(e);
}
Output:
try {
Try Block to check for errors.
}
catch(err) {
Catch Block to display errors.
}
finally {
Finally Block executes regardless of the try / catch
result.
}
©Topperworld
JavaScript
Example:
try {
console.log('try');
} catch (e) {
console.log('catch');
} finally {
console.log('finally');
}
Output:
©Topperworld