Implementing setTimeout() and setInterval() in pure JavaScript - javascript

I have some JavaScript benchmark code that is supported to be running on browser. But I would like to run it on console mode of JavaScript engine such as 'd8' in V8 for testing purpose.
I have written empty variables and functions for each DOM variables and functions(e.g. document.getElementById, etc.). But I cannot fully run the code since setTimeout() and setInterval() are supported by browser not from V8 engine. Is there a way to implement or simply emulate those functions in pure JavaScript code?
I appreciate any kind of comments.

You can't to that with just V8 + some JS, you have to embbed the JS engine in some kind of runtime that supports timers. For V8 there's zombie.js for instance.

Let's take a look how it is implemented in node timers.js. You can see at line #24, that timer_wrap binding is registered. This "internal module" is just a C library that supplies js module with time operations. That means that js timer implementation is based on some modules of an "upper level" and unfortunately can't be achieved in "pure js" as you wish.

Related

Is the concept of the "Execution context and the stack" is just for the browsers?

Is the concept of the "Execution context and the stack" is just for the browsers? or it's also implemented in other places such as NodeJS?
I've written 2 sentences and I'm not sure if they're correct:
1- "it’s not the “browser “who’s the environment for JavaScript! in other words: it’s not the entire browser! it’s just a part from the browser called The execution contexts."
2- All JavaScript code needs to run inside an environment and in the case of the browser this environment is called "execution contexts"
JavaScript typically runs inside a JavaScript engine.
As an example, Google's "V8 JavaScript Engine" powers javascript running in the Chrome browser, and in NodeJS.
Another example would be JavaScriptCore, Apple's engine, which runs javascript in the Safari browser and in various other contexts in MacOS / iOS.
Another example is Chakra, Microsoft's engine, which runs javascript in IE and Edge, or the javascript you would write in an "HTML application" published on XB1.
Even though JavaScript in Chrome and NodeJS run in the same engine, they have different additional modules and capabilities built around them -- things like the fs module in NodeJS, or access to the running DOM in the browser -- that are accessible from your code if the javascript agent provides it.
The "execution context and the stack", which describes the way JavaScript handles function calls, should be similar in every JavaScript engine because it is described by the ECMAScript spec. But if you get down into the nitty gritty, there can definitely be subtle differences in the way the stack is generated or formatted between V8 and JavaScriptCore, for example.
In short no, and you are mixing up different things.
In simplified terms, the execution context is a memory area accessible to the program. A program may consist of subprograms (class methods/functions/procedures) each of these has its own execution context to keep local variables local.
A stack in memory management is a mechanism used to keep track of different execution contexts. When you call a function, the current execution context gets pushed to a stack to offer a clean slate for the function to run. After the function is completed, the stack pops and returns the previous context.
These principles of memory management are true for computers, browsers, toasters, iPhones, and programs written in assembly language, and programs in Scratch.

Does Duktape has a Function for Executing an External Program in Linux?

I am using Duktape on Linux (Command Line, not Browser),
and would like my code to execute an external program in Linux, for example ls.
I went over Duktape's website, looking for Reference of the JavaScript functions supported by this Interpreter and Compiler,
but couldn't find any.
I did find there an API Reference, but that seems to be talking about calling functions that the Interpreter/Compiler library provides to outside callers,
and not the functions that are used inside a JavaScript program, that the Interpreter/Compiler runs..
Does any one know If there is a way to Execute an external program in Duktape in Linux,
and If yes, a 1-2 line sample code would be really helpful.
Thank you
Duktape is a pure JS execution environment. It does only provide functionality as defined in specific JS versions (mostly ES5, some ES6 and very few ES7). See also the Post ES5 status page.
Accessing the file system or starting processes is not part of the JS language defintion. For that you have to provide your own environment, which implements such functionality for scripts.

Array.filter function in serverside Javascript

I would like to filter an array in my XPage with serverside Javascript. Unfortunately I get the following error:
Error calling method 'filter(Function)' on an object of type 'Array [JavaScript Object]'
I have an Array of Strings like ["elem1","elem2","elem3"]
I call the function like this:
list.filter(function(){
});
Is there any reason why this error happens? Does this function even exist in ssjs?
This question is not duplicate since it is not clear that Xpages/Lotus Notes runs Rhino in background.
It sounds like whatever server-side JavaScript environment you're using doesn't support ES5 features (that's features from the 5th edition specification from December 2009).
You can use a polyfill for that and other things that were added to Array, see MDN, but beware: If ES5 features aren't supported, it's impossible to add things to Array.prototype without making them enumerable, meaning any code (mis)using for-in to loop through arrays will be affected.
There is Rhino behind XPages. And this SO topic
No Array.filter() in Rhino? says, it is out of date.
Edit:
No, it is not. Years ago I read somewhere about it. Now it seems it was not true. According to comment by Dan Sickles (quoting Philippe Riand?) here:
It runs on the server jvm and uses javascript as the application
language. For licensing reasons, IBM wrote their own jvm javascript
engine instead of using Rhino. With Rhino shipping in java 6, they
should be able to ship it in Designer 8.5 (or later). The licensing
problems may have been around the extensions like #Formulas and type
declarations. Classes, modules/namespaces and type declarations are
coming in javascript 2 and even google is helping to get that
implemented in Rhino. I'd hate to see a non-standard, javascript
engine underlying the coolest web development technology in Domino.
Speaking of Rhino, the "most important new feature that is not as
certain to be in 8.5 as XPages" uses Rhino and other jvm scripting
languages on the client. If this makes it into the product, two years
from now most new Notes applications will be written in neither
Lotusscript nor Java. I'll leave it at that.
In fact, there are few topics how to use Rhino in XPages, so with newest Rhino version your code would work. Anyway, my advice is to use Java calls.

Is there a javascript engine (runtime) that doesn't implement Timers interface

The Timers interface defined in HTML specification is a widely adopted interface among many javascript engine, including all browser's javascript engines (SpiderMonkey, V8, Chakra...) and the well known NodeJS (V8), but it is not a feature of javascript itself, so I'm wondering if there is any javascript engines that does not provide setTimeout and setInterval as a method of the global object..
Afaik jsdb doesn't provide setTimeout/setInterval. It does provide a [system.]sleep method.

Parallel JavaScript Code

Is it possible to run JavaScript code in parallel in the browser? I'm willing to sacrifice some browser support (IE, Opera, anything else) to gain some edge here.
If you don't have to manipulate the dom, you could use webworkers ... there's a few other restrictions but check it out # http://ejohn.org/blog/web-workers/
Parallel.js of parallel.js.org (see also github source) is a single file JS library that has a nice API for multithreaded processing in JavaScript. It runs both in web browsers and in Node.js.
Perhaps it would be better to recode your JavaScript in something that generally runs faster, rather than trying to speed up the Javascript by going parallel. (I expect you'll find the cost of forking parallel JavaScript activities is pretty high, too, and that may well wipe out any possible parallel gain; this is common problem with parallel programming).
Javascript is interpreted in most browsers IIRC, and it is dynamic on top of it which means it, well, runs slowly.
I'm under the impression you can write Java code and run it under browser plugins. Java is type safe and JIT compiles to machine code. I'd expect that any big computation done in Javascript would run a lot faster in Java. I'm not specifically suggesting Java; any compiled language for which you can get a plug in would do.
As an alternative, Google provides Closure, a JavaScript compiler. It is claimed to be a compiler, but looks like an optimizer to me and I don't know much it "optimizes". But, perhaps you can use that. I'd expect the Closure compiler to be built into Chrome (but I don't know for a fact) and maybe just running Chrome would get your JavaScript compiler "for free".
EDIT: After reading about what about Closure does, as compiler guy I'm not much impressed. It looks like much of the emphasis is on reducing code size which minimizes download time but not necessarily performance. The one good thing they do in function inlining. I doubt that will help as much as switching to a truly compiled langauge.
EDIT2: Apparantly the "Closure" compiler is different than the engine than runs JavaScript in Chrome. I'm told, but don't know this for a fact, that the Chrome engine has a real compiler.
Intel is coming up with an open-source project codenamed River Trail check out http://www.theregister.co.uk/2011/09/17/intel_parallel_javascript/

Categories