Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sortipv4
(version: 0)
sort
Comparing performance of:
sort1 vs sort2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function sort1(a, b) { // ip 排序 const num1 = Number( (a.ip || (typeof a === "string" && a) || "") .split(".") .map((num) => `000${num}`.slice(-3)) .join("") ); const num2 = Number( (b.ip || (typeof b === "string" && b) || "") .split(".") .map((num) => `000${num}`.slice(-3)) .join("") ); return num1 - num2; } function sort2(a, b) { // ip 排序 const arr1 = (a.ip || (typeof a === "string" && a) || "").split("."); const arr2 = (b.ip || (typeof b === "string" && b) || "").split("."); for (let i = 0; i < arr1.length; i++) { const num1 = Number(arr1[i]); const num2 = Number(arr2[i]); if (num1 !== num2) { return num1 > num2 ? 1 : -1; } } return 0; }
Tests:
sort1
for(let i = 0; i < 10000; i ++) { sort1({ip: "192.168.0.1"}, {ip: "192.168.0.254"}); sort1({ip: "192.168.254.1"}, {ip: "192.168.254.254"}); sort1({ip: "111.111.111.111"}, {ip: "222.222.222.222"}); sort1({ip: "222.222.222.222"}, {ip: "111.111.111.111"}); sort1({ip: "11.11.11.11"}, {ip: "22.22.22.22"}); sort1({ip: "22.22.22.22"}, {ip: "11.11.11.11"}); sort1({ip: "1.1.1.1"}, {ip: "2.2.2.2"}); sort1({ip: "2.2.2.2"}, {ip: "1.1.1.1"}); sort1({ip: "1.1.1.1"}, {ip: "1.1.1.1"}); sort1({ip: "11.11.11.11"}, {ip: "11.11.11.11"}); sort1({ip: "111.111.111.111"}, {ip: "111.111.111.111"}); sort1({ip: "100.1.1.1"}, {ip: "1.1.1.1"}); sort1({ip: "1.100.1.1"}, {ip: "1.1.1.1"}); sort1({ip: "1.1.100.1"}, {ip: "1.1.1.1"}); sort1({ip: "1.1.1.100"}, {ip: "1.1.1.1"}); }
sort2
for(let i = 0; i < 10000; i ++) { sort2({ip: "192.168.0.1"}, {ip: "192.168.0.254"}); sort2({ip: "192.168.254.1"}, {ip: "192.168.254.254"}); sort2({ip: "111.111.111.111"}, {ip: "222.222.222.222"}); sort2({ip: "222.222.222.222"}, {ip: "111.111.111.111"}); sort2({ip: "11.11.11.11"}, {ip: "22.22.22.22"}); sort2({ip: "22.22.22.22"}, {ip: "11.11.11.11"}); sort2({ip: "1.1.1.1"}, {ip: "2.2.2.2"}); sort2({ip: "2.2.2.2"}, {ip: "1.1.1.1"}); sort2({ip: "1.1.1.1"}, {ip: "1.1.1.1"}); sort2({ip: "11.11.11.11"}, {ip: "11.11.11.11"}); sort2({ip: "111.111.111.111"}, {ip: "111.111.111.111"}); sort2({ip: "100.1.1.1"}, {ip: "1.1.1.1"}); sort2({ip: "1.100.1.1"}, {ip: "1.1.1.1"}); sort2({ip: "1.1.100.1"}, {ip: "1.1.1.1"}); sort2({ip: "1.1.1.100"}, {ip: "1.1.1.1"}); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sort1
sort2
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 dive into the world of MeasureThat.net and explore the provided benchmark. **Benchmark Definition JSON** The benchmark definition defines two functions, `sort1` and `sort2`, which are used to sort IP addresses in a specific format. The `Script Preparation Code` section contains the implementation details for these functions. **Function Purpose** Both functions take two objects as input, each containing an `ip` property representing an IP address. They return an integer value indicating the order of the IP addresses. `sort1` uses string manipulation to extract the numeric part of the IP address (in the format `xxx.xxx.xxx.xxx`) and then compares these values using subtraction. `sort2` iterates through the parts of the IP address, converts each part to a number, and compares these numbers using a simple comparison logic. **Comparison Options** The benchmark is comparing two approaches: 1. **String manipulation with subtraction (`sort1`)**: This approach uses string manipulation to extract the numeric part of the IP address and then compares these values using subtraction. 2. **Iterating through parts and comparing numbers (`sort2`)**: This approach iterates through the parts of the IP address, converts each part to a number, and compares these numbers using a simple comparison logic. **Browser Benchmark Results** The latest benchmark results show the execution speed for both functions on a Chrome 114 browser running on Windows: * `sort2` (iteration-based approach): 6.964 executions per second * `sort1` (string manipulation with subtraction): 5.487 executions per second Based on these results, it appears that the iteration-based approach (`sort2`) is slightly faster than the string manipulation approach (`sort1`).
Related benchmarks:
Javascript Sorting Algorithms
Javascript Sorting Algorithmzzz
Javascript Sorting Algorithms large
sortipv4ModifyTestcase
Comments
Confirm delete:
Do you really want to delete benchmark?