Minified variable for javascript forloop is wrongly minified? - javascript

I am using UglifyJS to minify js. I've noticed that:
var start = 0;
for( var t = start; t<k.length; t++)
The t is replaced with
var t=!0
What is going on here? I know !0 === true, but this is throwing errors for IE8. It doesn't throw any error for other modern browsers but I am not sure they are actually working as expected.
Edit: example here
var start = 0;
function scan() {
var hello = true;
for (var j = start; j < 3; j++) {
}
return hello;
}
After minify using Uglify
function scan(){for(var a=!0,b=start;3>b;b++);return a}var start=0;

Related

java 8 nashorn engine is really slow

I am running the following javascript test
var mark = java.lang.System.nanoTime() / 1000000000.0;
for(var i = 0; i != 1000; i++) {
}
var now = java.lang.System.nanoTime() / 1000000000.0;
var e = now - mark;
print(1 / e);
and get this result
27.361456496425802
this seems really slow almost a bug or something i am doing wrong. Here is the java code
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String[] lines = IO.readAllLines(IO.file(root, "LoadList.txt"));
String script = "";
for(int i = 0; i != lines.length; i++) {
String file = lines[i].trim();
if(!file.startsWith("#") && file.length() != 0) {
script += IO.readAllText(IO.file(root, file));
}
}
engine.eval(script);
} catch(Exception ex) {
ex.printStackTrace();
}
does anybody know what i am doing wrong or have seen this type of problem and know how to fix if?
Thanks in advance
after doing some research it appears that java's javascript takes a little while to warm up to speed. here is an update to the test
for(var i = 0; i != 1000; i++) {
var mark = java.lang.System.nanoTime() / 1000000000.0;
for(var j = 0; j != 1000; j++) {
}
var now = java.lang.System.nanoTime() / 1000000000.0;
var e = now - mark;
print(1 / e + " fps")
}
and by the last iteration i get 99108.03064699778 fps. (it does vary the lowest one i got, on the last iteration, was around 50000 fps)

Custom function for Google Spreadsheet always return the same result

I am trying to build a simple script to work with a Google Spreadsheet that looks like this:
It shows me which of my clients use which modules of each layer. Each client may use how many modules he wants to.
What I need to do is count how many clients have all the modules installed (or the three layers, it's the same).
I've tried to do this using the built-in functions but have not succeed.
Now I'm trying to do my own function, that looks like this:
function countTotalModByClient(values) {
var quantMod=0;
var quantClient=0;
for (var i=0; i<values.length; i++) {
for(var j=0; j<values.length; j++) {
if(values[i][j]=="X") {
quantMod++;
}
}
if(quantMod==15) { // total number of modules
quantClient++;
}
quantMod=0;
}
return quantClient;
}
But it always return the same result: 0.
At my sheet, I'm calling the function like this: =countTotalModByClient(B3:P6)
P.S.: Sorry about the magic number in the code, I´ll fix this. =)
This should be possible with a standard formula (although maybe a little complex).
=countif(ArrayFormula(MMULT(--(B3:P6="X"), transpose(column(B3:P2)^0))), 15)
should return the number of clients with a count of 15 in their row..
Can you check if that works ?
Or if you prefer a custom function, give this a try:
function countTotalModByClient(values) {
var quantClient = 0;
for (var i = 0; i < values.length; i++) {
if (values[i].countItem("X") === 15) quantClient += 1;
}
return quantClient;
}
Array.prototype.countItem = function (item) {
var counts = {};
for (var i = 0; i < this.length; i++) {
var num = this[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
return counts[item] || 0;
}
Example spreadsheet

Breaking out of nested loops: return or label/break?

I am using a JavaScript function to set a global variable. Below, I have two really dumb example functions. One uses a label to break out of the nested loops. The other uses an empty return.
My question: which is better from a performance issue? (For the sake of argument, lets say you did this a few million times.)
Using empty return
function foo() {
for(var i = 0; i < 100; ++i) {
for(var j = 0; j < 100; ++j) {
if(i * j == 50) {
myGlobal = j;
return;
}
}
}
}
Using label and break
function foo() {
dance:
for(var i = 0; i < 100; ++i) {
for(var j = 0; j < 100; ++j) {
if(i * j == 50) {
myGlobal = j;
break dance;
}
}
}
}
I know that I will be doing nothing except finishing the function after my inner condition is met/I make my assignment.
Thanks!
After some testing (via Chrome console, MBP 2013, OSX 10.9, Intel i7 # 2.8GHz, 16GB DDR3), the results are very interesting. I ran two types of tests. The first tested using return and label/break to break out of a nested loop. The second used a straight return and label/break, with nothing else in the function. The test code:
function r() {
for(var i = 0; i < 10; ++i) {
for(var j = 0; j < 10; ++j) {
if(i*j == 50) {
return;
}
}
}
}
function b() {
dance:
for(var i = 0; i < 10; ++i) {
for(var j = 0; j < 10; ++j) {
if(i*j == 50) {
break dance;
}
}
}
}
function r2() {
return;
}
function b2() {
dance:
break dance;
}
var startTime;
var endTime;
console.log("Return test");
startTime = Date.now();
for(var i = 0; i < 1000000000; ++i) {
r2();
}
endTime = Date.now();
console.log(endTime - startTime);
console.log("Break test");
startTime = Date.now();
for(var i = 0; i < 1000000000; ++i) {
b2();
}
endTime = Date.now();
console.log(endTime - startTime);
When comparing breaking out of a the nested loops (functions r() and b() ), the return consistently performed significantly better. However, when using just the return or label/break in the function (functions r2() and b2() ) the label/break performed significantly faster. Test result breakdown:
Test 1, using 10000000 iterations
Average runtime (milliseconds) after 3 runs of using return to leave nested loops: 1215ms
Average runtime (milliseconds) after 3 runs of using label/break to leave nested loops: 1522ms
Test 2, using 1000000000 iterations //2 orders of magnitude more iterations
Average runtime (milliseconds) after 3 runs of using return: 1879ms
Average runtime (milliseconds) after 3 runs of using label/break: 1862ms
Thus:
For breaking nested loops, using return is ~25% faster
For science/HPC, using label/break is ~1% faster
I personally don't see anything wrong with empty return statements to abort execution early for a function that doesn't return anything normally. It's certainly cleaner than a label, and it's more language-agnostic too. A lot of languages don't support labeled for loops like whatever language your example is in, so the empty return statement will be simpler to understand for people coming from other languages lacking that feature.
Both have the same performance; the former arguably is more readable.
However, the latter makes it easier to modify the function should you need to add more instructions in the future after the loops.
UPDATE:
Good info here: Why JavaScript functions always return a value?, first answer says: 'Thus, return and function-executed-to-its-end semantics match.' So even if you use break, at the end of the execution it will return the same as if you use return

Does JavaScript cache/optimize code if repeated multiple times?

I'm currently writing a small framework to test the speed of JavaScript functions. When I repeatedly call the same methods with the same parameter, it gives me strange results:
Function Execution Time
isEvenBitwise 38.00000000046566
isEvenModulo 38.00000000046566
isEvenPointless 38.00000000046566
Here are my functions:
var myFunctions =
{
isEvenBitwise: function(number)
{
return !(number & 1);
},
isEvenModulo: function(number)
{
return (number % 2 == 0);
},
isEvenPointless: function(number)
{
return 1;
}
}
The code that runs the functions:
PerformanceTest.prototype.measureTime = function()
{
for (var indexTests = 0; indexTests < this.testCount; indexTests++)
{
var results = [];
for (var currentFunction in this.functions) {
var contextFunction = this.functions[currentFunction];
var startTime = performance.now();
for (var i = 0; i < this.iterationsPerTest; i++)
{
var heh = contextFunction.apply(this, arguments)
}
var executionTime = performance.now() - startTime;
var result = {};
result.testName = currentFunction;
result.executionTime = executionTime;
results.push(result);
}
this.testResults.push(results);
}
}
Does the JavaScript interpreter cache/optimize my code? If so, how does it work? Or is there anything else happening I'm not aware of?
Edit: This seems to occur only in chrome, firefox works just fine with these results:
Function Execution Time
isEvenBitwise 9.652258097220447
isEvenModulo 37.546061799704376
isEvenPointless 8.512472488871936
After looking at your code I am going to make a guess that Chrome is being smart about what you are doing. It is seeing this:
var startTime = performance.now();
for (var i = 0; i < this.iterationsPerTest; i++)
{
var heh = contextFunction.apply(this, arguments)
}
var executionTime = performance.now() - startTime;
It is correctly assessing that the contextFunction has no side effects, recognising that the heh variable exists only within the loop scope and is never used and then optimising the entire loop away because it doesn't do anything.

Why this JS function does't not work when it calls by selenium.getEval()

Below js function is working well in html page. But when i put it into selenium.getEval in RC to wonder to get return value j. It cannot work, also without any error. Do i miss some function call format? Thanks in advance!!!
function GetTableRow()
{
var table;
var ctext = "bb";
var cname = "mps_tableborder";
var col = 0;
var j;
for (i = 0; i < document.getElementsByTagName("table").length; i++) {
if (cname==document.getElementsByTagName("table")[i].className) {
table = document.getElementsByTagName("table")[i];
break;
}
}
for (j = 0; j < table.rows.length; j++){
if (ctext == table.rows[j].cells[col].innerText){
break;
}
}
return (j);
}
You can provide only the contents of method. Not the entire method. Try by removing the method starting [ function GetTableRow() { ] and method ending [ } ] and give it to getEval method and see what it returns! Ideally it should work fine.

Categories

Resources