-
-
Save jridgewell/13276efa27fa2bcfedb1fadea18ccb3c to your computer and use it in GitHub Desktop.
getCookie (split vs regex) (http://jsbench.github.io/#13276efa27fa2bcfedb1fadea18ccb3c) #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>getCookie (split vs regex)</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 () { | |
| var docCookieShort = 'abc = 123'; | |
| var docCookieLong = 'test = ing; 123 = test; 123 = test; 123 = test; 123 = test; abc = 123 ;test = ing; 123 = test; 123 = test; 123 = test; 123 = test;'; | |
| function getCookie(docCookie, name) { | |
| const cookies = docCookie.split(';'); | |
| for (let i = 0; i < cookies.length; i++) { | |
| const cookie = cookies[i].trim(); | |
| const eq = cookie.indexOf('='); | |
| if (eq == -1) { | |
| continue; | |
| } | |
| if (cookie.substring(0, eq).trim() == name) { | |
| return cookie.substring(eq + 1).trim(); | |
| } | |
| } | |
| return null; | |
| } | |
| function regexCookie(docCookie, name) { | |
| const whitespace = '\\s\\uFEFF\\xA0' | |
| const regex = new RegExp(`(^|;)[${whitespace}]*${name}[${whitespace}]*=[${whitespace}]*([^${whitespace};])*`); | |
| const match = regex.exec(docCookie); | |
| if (!match) { | |
| return null; | |
| } | |
| return match[2]; | |
| } | |
| }; | |
| suite.add("Split (short)", function () { | |
| // Split (short) | |
| getCookie(docCookieShort, 'abc'); | |
| }); | |
| suite.add("Regex search (short)", function () { | |
| // Regex search (short) | |
| regexCookie(docCookieShort, 'abc'); | |
| }); | |
| suite.add("Split (long)", function () { | |
| // Split (long) | |
| getCookie(docCookieLong, 'abc'); | |
| }); | |
| suite.add("Regex search (long)", function () { | |
| // Regex search (long) | |
| regexCookie(docCookieLong, 'abc'); | |
| }); | |
| 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("getCookie (split vs regex)"); | |
| 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