Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hash-sum string vs number 2
(version: 1)
The hash-sum module returns strings. Is it faster to return numbers? https://github.com/bevacqua/hash-sum
Comparing performance of:
String vs Number
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function pad(hash, len) { while (hash.length < len) { hash = "0" + hash; } return hash; } function fold(hash, text) { var i; var chr; var len; if (text.length === 0) { return hash; } for (i = 0, len = text.length; i < len; i++) { chr = text.charCodeAt(i); hash = (hash << 5) - hash + chr; hash |= 0; } return hash < 0 ? hash * -2 : hash; } function foldObject(hash, o, seen) { return Object.keys(o).sort().reduce(foldKey, hash); function foldKey(hash, key) { return foldValue(hash, o[key], key, seen); } } function foldValue(input, value, key, seen) { var hash = fold(fold(fold(input, key), toString(value)), typeof value); if (value === null) { return fold(hash, "null"); } if (value === undefined) { return fold(hash, "undefined"); } if (typeof value === "object" || typeof value === "function") { if (seen.indexOf(value) !== -1) { return fold(hash, "[Circular]" + key); } seen.push(value); var objHash = foldObject(hash, value, seen); if (!("valueOf" in value) || typeof value.valueOf !== "function") { return objHash; } try { return fold(objHash, String(value.valueOf())); } catch (err) { return fold( objHash, "[valueOf exception]" + (err.stack || err.message) ); } } return fold(hash, value.toString()); } function toString(o) { return Object.prototype.toString.call(o); } function sumStr(o) { return pad(foldValue(0, o, "", []).toString(16), 8); } function sumNum(o) { return foldValue(0, o, "", []); } var hash1 = sumStr; var hash2 = sumNum; var object = { glossary: { title: "example glossary", GlossDiv: { title: "S", GlossList: { GlossEntry: { ID: "SGML", SortAs: "SGML", GlossTerm: "Standard Generalized Markup Language", Acronym: "SGML", Abbrev: "ISO 8879:1986", GlossDef: { para: "A meta-markup language, used to create markup languages such as DocBook.", GlossSeeAlso: ["GML", "XML"], }, GlossSee: "markup", }, }, }, }, };
Tests:
String
let s = hash1(object);
Number
let n = hash2(object);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String
Number
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Overview** The benchmark, called "Hash-sum string vs number 2", is designed to compare the performance of two different approaches for summing up values using the hash-sum module. The module returns strings, but there are two options: one that returns numbers ( `sumNum` ) and another that returns strings ( `sumStr` ). **Benchmark Definition** The benchmark definition is a JSON object that contains: * A name and description of the benchmark. * A script preparation code that defines several functions: + `pad`: pads a string with leading zeros to a specified length. + `fold`: performs a folding operation on a hash value based on a string or number input. + `foldObject`: recursively folds an object's keys into a single hash value. + `foldValue`: handles the actual folding of values, including handling circular references and exceptions. + `toString`: returns the string representation of an object using `Object.prototype.toString`. * A script preparation code that defines two functions: + `sumStr`: sums up a value using the `foldValue` function and returns a padded hexadecimal string. + `sumNum`: sums up a value using the `foldValue` function without padding and returns a number. **Test Cases** There are two test cases: * "String": executes the `let s = sumStr(object);` line. * "Number": executes the `let n = sumNum(object);` line. These test cases compare the performance of `sumStr` and `sumNum`. **Library** The hash-sum module is used in this benchmark. Its purpose is to efficiently calculate a digital fingerprint (hash) or summary value for input data, such as strings or numbers. **Special JS feature or syntax** None mentioned explicitly in this explanation. However, the use of `Object.prototype.toString` and the handling of circular references using `seen.push(value)` in the `foldValue` function demonstrate some JavaScript features. **Performance considerations** The performance difference between `sumStr` and `sumNum` likely comes from the padding step in `sumStr`, which adds leading zeros to the hexadecimal string output. This could potentially increase the size of the output, affecting performance compared to returning a pure number. Here are some pros and cons of each approach: * **`sumStr`**: + Pros: handles circular references correctly, provides a human-readable format (hexadecimal string), and is likely more suitable for strings. + Cons: adds leading zeros to the output, potentially increasing size and performance impact. * **`sumNum`**: + Pros: returns a pure number, potentially faster due to reduced overhead of padding. + Cons: may not handle circular references as robustly, provides less human-readable output. Other alternatives for this benchmark could include: * Using a different hashing algorithm or library (e.g., `crypto-js`, `js-sha256`). * Comparing the performance of different folding techniques or data structures. * Adding more test cases to cover other edge cases or scenarios.
Related benchmarks:
JS native vs Ramda vs. Lodash
JS native vs js native loop vs Ramda vs. Lodash
JS native vs js native loop vs Ramda vs. Lodash 2
JS native vs js native loop vs Ramda vs. Lodash 4
Is odd package vs simple function
Comments
Confirm delete:
Do you really want to delete benchmark?