Javascript's performance.now() and Nodejs [duplicate] - javascript

This question already has answers here:
window.performance.now() equivalent in nodejs?
(9 answers)
Closed 5 years ago.
I'm new to Nodejs and I'm confused about something when timing:
If Nodejs is Javascript, then why Javascript's performance.now() doesn't work on Nodejs and I'm forced to use console.time() or the like?
How can I know what other Javascript functions doesn't work on Node?
Thanks!

Update: Although this was correct at the time it was written, this answer is now obsolete and modern versions of Node do support performance.now()
First, we have to be very clear about what JavaScript does -- and does not include.
The definition for what JavaScript includes is the EMCA-262 guide. This is the only thing that JavaScript is required to have.
Everything else - from the DOM to the performance object are additions provided by the host environment.
Some -- many -- browsers have chosen to add performance.now() but that is NOT part of JavaScript -- just something that JavaScript can call.
nodeJS currently doesn't support it -- at least not out-of-the-box, but it looks like someone created a module that does give you that ability.
I've not tried it, just did a quick google for 'node performance.now` and this was the first hit: https://www.npmjs.com/package/performance-now

Related

Colon after colon syntax while calling a function in Javascript [duplicate]

This question already has an answer here:
Colon after function declaration in javascript [duplicate]
(1 answer)
Closed 4 years ago.
I came across a syntax somewhere on the web recently and couldn't grasp its meaning.
What I understand is that when we write props: Object inside the brackets, it means we're assigning a default value to props as Object. But what does the 2nd colon signify? It looks like a key-value pair but is confusing me still.
Tried searching on the web but wasn't able to search due to lack of terminology. Any ideas what this means?
someFn(props: Object): Object {
return someOtherFn(props);
}
These are type annotations. They are not standard javascript. They are added when using tools that layer static typing onto javascript. The two most popular flavors are Typescript and Flow.
When you write code that uses this syntax you will transpile your source code into code that is syntactically valid for execution by running one of the above mentioned tools on your code. When you do, it will tell you if your usage of the types is correct, raise warnings that are helpful in development, and then strip all this out so it can actually be run.

Where can i find the code source of javascript functions? [duplicate]

This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Closed 1 year ago.
For example, i want to know how the querySelector method was built. I checked on MDN but there's only examples that show how to use it.
I also tried to check in the console.log but nothing readable.
There is a non-standard method to do this. Please be aware that it is not fully cross-browser compatible and shouldn't be used in a production environment.
function hello(){
return "Hi!";
}
console.log(hello.toSource());
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toSource for more.
Edit:
To see the source code of a built-in function, you will need to look at the engine source. This varies browser to browser. For example, Chrome uses the V8 engine. See https://github.com/v8/v8

How to detect if browser is compatible with ES2015 [duplicate]

This question already has answers here:
Javascript ES6 cross-browser detection
(10 answers)
Closed 7 years ago.
I have a big chunk of JS libraries that I should rewrite since it's really old and outdated. So, I decided to come up with a solution where I would just use most of ES2015 features like rest parameters.
The thing is, I am sure all the clients would not have their browser up-to-date and I am confused whether I will face any issue regarding their browser is compatible with my new JS libs.
So, I was wondering if I could detect whether the client browsers are compatible with ES2015. And if not, I would just include my old JS library.
I am looking for a solution like Conditional comments, but I am getting nowhere to solution.
Any help in HTML, JS or PHP is appreciated. Please kindly suggest your advice.
I was wondering if I could detect whether the client browsers are
compatible with ES2015. And if not, I would just include my old JS
library.
You cannot do that, simply because AFAIK there's no browser that fully supports ES2015. Also, you don't really want to maintain two different versions of your code, because it's painful and it could get messy really quick.
The approach nowadays is to use a transpiler, which is sort of a compiler that compiles your ES2015 code to ES5 (the JavaScript that every browser knows). It is still kind of messy, but at least you get to write only one version of your code, and it's ES2015.
I think Babel (formerly 6to5) is the most popular transpiler. You can check out their website for getting started.
As to answer your actual question,
How to detect if browser is compatible with ES2015
You can do that in many ways. I'm not sure what could be the most reliable one, but for example you could simply try on your browser's console:
'Promise' in window
If this statement returns true, then the current browser supports promises, which is a new feature of ES2015, so you could assume that it supports most of the features of ES2015.
This is not enough for most cases though; you may want to be 100% sure that what you're using is not going to throw a SyntaxError in any old browser.
A solution could be to manually check for each feature you want to use. For example, if you need the Fetch API, you could create a function that tells you if the current browser supports it:
function isFetchAPISupported() {
return 'fetch' in window;
}
You can do this for any new API you need. However, if you need something syntactically different, I think your only bet is eval() (as Katana314 suggested). For example, in order to check support for rest parameters you could do:
function isRestSupported() {
try {
eval('function foo(bar, ...rest) { return 1; };');
} catch (error) {
return false;
}
return true;
}
This works great in Firefox, because the rest parameters are supported.
It works as well on Safari, returning false because rest parameters are not supported there yet.

What's the purpose of Facebook's __d method? [duplicate]

This question already has answers here:
What is Facebook's function __d
(3 answers)
Closed 9 years ago.
I've noticed something interesting what Facebook do, and I'd like to know what they are doing and why? If you look at their source code on one of their .js files they seem to be doing a lot of this:
__d("AjaxRequest",["Erro...
__d("FBAjaxRequest",["AjaxRequest"...
__d("CallbackManagerController",...
There seems to be no variables or ordinary functions and objects in any of their JavaScript files. There are functions, but there all arguments passed to this __d method, like this:
__d("keyMirror",[],function(a,b,c,d,e,f){var g=function(h){var i={},j;if(!h)return h;for(j in h){if(!h.hasOwnProperty(j))continue;i[j]=j;}return i;};e.exports=g;});
Is this some sort of optimization for JavaScript, or is it used for easier management?
Thanks
Maybe just a wrapper that does things like dependency injection and error handling. If you can find out where __d is define, you'll know for sure.
It doesn't look like an optimizing (ie. a packer).

Good Javascript Cleaner [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
HTML formatter/tidy/beautifier for JavaScript
I'm looking for a good automated javascript cleaner (as in locates and fixes errors), similar to the w3c validator's cleaner function (that fixes the javascript errors).
Update: Thanks to a helpful responce I found my solution: Closure Linter.
Try jsbeautifier to beautify your code and jslint to clean up its syntax.
If I understand you correctly, you might be interested in Google's Closure Linter, which, like JSLint, validates your code and reports problems, but also provides the possibility to fix them:
(...) you can substitute fixjsstyle for gjslint to automatically fix many of the errors that gjslint checks for.
I quite like http://jsbeautifier.org/ for cleaning up Javascript.
This site is also mentioned in the answer provided by Matt in the comments.

Categories

Resources