Skip to content

Instantly share code, notes, and snippets.

@oleksify
Forked from jenniferlynparsons/index.html
Created November 8, 2019 14:39
Show Gist options
  • Select an option

  • Save oleksify/bae346a0642b418b8c9f434155220156 to your computer and use it in GitHub Desktop.

Select an option

Save oleksify/bae346a0642b418b8c9f434155220156 to your computer and use it in GitHub Desktop.
getLongestUniformSequenceRange (http://jsbench.github.io/#bae346a0642b418b8c9f434155220156) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>getLongestUniformSequenceRange</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>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
suite.add("const getLongestUniformSequenceRange = (s) => {", function () {
const getLongestUniformSequenceRange = (s) => {
let maxStartIndex = 0
let startIndex = 0
let maxSeqLength = 1
let seqLength = 1
for (let i = 0; i < s.length; i++) {
const currChar = s.charAt(i)
const nextChar = s.charAt(i + 1)
if (currChar === nextChar) {
seqLength++
if (seqLength > maxSeqLength) {
maxSeqLength = seqLength
maxStartIndex = startIndex
}
} else {
seqLength = 1
startIndex = i + 1
}
}
return [maxStartIndex, maxStartIndex + maxSeqLength - 1]
}
getLongestUniformSequenceRange('aabbbcc')
});
suite.add("const getLongestUniformSequenceRange = s => {", function () {
const getLongestUniformSequenceRange = s => {
if (s.length <= 1) return [0, 0]
let startIndex = 0
let seqLength = 1
let longestSeq = 0
for (let i = 0; i < s.length; i++) {
const currChar = s.charAt(i)
const nextChar = s.charAt(i + 1)
if (currChar === nextChar) {
seqLength++
} else if (seqLength > 1) {
if (seqLength > longestSeq) {
longestSeq = seqLength
startIndex = s.indexOf(currChar.repeat(seqLength))
}
if (seqLength > s.length - i) break
seqLength = 1
}
}
return [startIndex, startIndex + seqLength - 1]
}
getLongestUniformSequenceRange('aabbbcc')
});
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("getLongestUniformSequenceRange");
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