Last active
June 5, 2020 21:31
-
-
Save DerekZiemba/79c073db881ec273a6ef630cfc258e49 to your computer and use it in GitHub Desktop.
Javascript Fastest way to check for String Type that is not empty #jsbench #jsperf (https://jsbench.github.io/#79c073db881ec273a6ef630cfc258e49) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Javascript Fastest way to check for String Type that is not empty #jsbench #jsperf</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
function sampleBuilder(root, count) { | |
count = count || 10000; | |
const results = []; | |
function addToResult(obj) { | |
try { | |
if (obj == null || obj.constructor) { // Verify we don't get an illegal access error | |
results.push(obj); | |
} | |
} catch (e) { } | |
} | |
function recurser(obj) { | |
addToResult(obj); | |
if (!obj || results.length >= count) { | |
return; | |
} else if (Array.isArray(obj)) { | |
obj.forEach(recurser); | |
return; | |
} | |
var keys = Object.keys(obj); | |
for (var len = keys.length, i = 0; i < len; ++i) { | |
if (results.length >= count) { return; } | |
try { | |
var item = obj[keys[i]]; | |
if (item && results.indexOf(item) === -1) { | |
switch (results.length % 15) { | |
case 7: results.push(null); break; | |
case 11: results.push(undefined); break; | |
} | |
switch (typeof item) { | |
case 'function': | |
switch (results.length % 15) { | |
case 11: results.push(new String(item)); break; | |
case 13: results.push(item.toString()); break; | |
} | |
case 'object': | |
recurser(item); | |
break; | |
default: | |
addToResult(item); | |
} | |
} | |
} catch (e) { | |
results.push(e); | |
} | |
} | |
} | |
recurser(root); | |
return results; | |
} | |
var sampleObjects = sampleBuilder(window, 2500); | |
}; | |
suite.add("typeof", function () { | |
// typeof | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (typeof obj === 'string' && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.add("instanceof", function () { | |
// instanceof | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (obj instanceof String && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.add("implementation", function () { | |
// implementation | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if ((typeof obj === 'string' || obj instanceof String) && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.add("implementation2", function () { | |
// implementation2 | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (obj !== null && obj !== undefined && (typeof obj === 'string' || obj instanceof String) && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.add("isStringOnly", function () { | |
// isStringOnly | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (obj instanceof String || typeof obj === 'string') { ++count; } | |
} | |
return count; | |
}); | |
suite.add("propCheck", function () { | |
// propCheck | |
var count = 0, objs = sampleObjects; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (obj != null && obj.substring && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.add("constructor", function () { | |
// constructor | |
var count = 0, objs = sampleObjects, StringClass = String; | |
for(var len = objs.length, i = 0; i < len; ++i){ | |
var obj = objs[i]; | |
if (obj != null && obj.constructor === StringClass && obj.length > 0) { ++count; } | |
} | |
return count; | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Javascript Fastest way to check for String Type that is not empty #jsbench #jsperf"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment