var arr = [ "93", "53", "45", "75", "6", "65", "60", "30", "59", "5", "41", "17", "36", "23", "45", "71", "22", "15", "77", "75", "7", "74", "69", "36", "96", "2", "37", "25", "8", "79", "80", "85", "35", "66", "14", "3", "51", "32", "87", "41", "78", "44", "79", "93", "1", "81", "44", "9", "51", "94", "28", "84", "87", "63", "56", "75", "59", "57", "83", "34", "21", "19", "15", "91", "27", "70", "53", "32", "32", "6", "60", "10", "46", "25", "80", "32", "67", "92", "12", "34", "83", "9", "59", "99", "87", "39", "68", "62", "81", "43", "34", "88", "78", "86", "11", "42", "50", "57", "66", "1", "83", "58", "82", "3", "34", "46", "76", "18", "8", "1", "33", "97", "31", "88", "6", "67", "33", "85", "3", "84", "54", "44", "93", "46", "98", "31", "75", "27", "63", "1", "8", "80", "49", "54", "83", "19", "9", "11", "41", "4", "65", "78", "8", "37", "61", "22", "90", "89", "1", "79", "35", "37", "4", "60", "49", "45", "8", "11", "3", "83", "54", "1", "72", "28", "41", "51", "66", "77", "31", "3", "47", "17", "58", "96", "37", "32", "26", "62", "10", "39", "35", "29", "85", "99", "55", "76", "15", "2", "10", "89", "70", "30", "1", "100", "38", "46", "84", "95", "33", "38", "78", "42", "60", "27", "70", "92", "41", "3", "21", "38", "7", "17", "11", "56", "17", "27", "56", "12", "73", "91", "63", "18", "87", "32", "88", "66", "70", "28", "12", "48", "93", "99", "82", "82", "35", "10", "10", "42", "99", "26", "63", "88", "37", "55", "95", "12", "72", "80", "82", "75", "55", "71", "87", "52", "57", "87", "88", "39", "77", "47", "26", "49", "41", "24", "42", "74", "58", "14", "87", "89", "40", "78", "69", "53", "95", "36", "95", "48", "66", "38", "100", "72", "40", "90", "21", "27", "20", "2", "95", "40", "30", "84", "37", "80", "10", "58", "47", "50", "54", "57", "62", "98", "22", "53", "19", "7", "78", "62", "84", "40", "95", "49", "99", "72", "90", "47", "26", "16", "72", "69", "88", "6", "21", "60", "91", "41", "43", "98", "95", "22", "31", "21", "15", "82", "31", "95", "38", "11", "53", "38", "76", "60", "20", "58", "14", "53", "6", "9", "19", "92", "64", "50", "64", "19", "82", "17", "13", "86", "45", "21", "79", "89", "86", "43", "13", "27", "78", "2", "73", "16", "48", "51", "64", "22", "78", "52", "16", "84", "23", "60", "60", "42", "51", "99", "99", "93", "97", "64", "53", "35", "39", "11", "25", "19", "88", "18", "86", "55", "91", "20", "9", "95", "11", "74", "34", "10", "64", "4", "3", "53", "33", "82", "71", "66", "52", "45", "17", "18", "28", "26", "12", "47", "2", "65", "38", "52", "49", "34", "68", "84", "5", "79", "82", "51", "73", "82", "39", "10", "38", "84", "32", "15", "17", "76", "26", "51", "30", "46", "95", "96", "7", "5", "18", "14", "12", "23", "99", "74", "3", "52", "68", "7", "35", "82", "2", "35", "44", "49", "31", "88", "77", "32", "75", "23", "25", "37", "28", "59", "90", "60", "22", "87", "56", "89", "97", "78", "23", "14", "40", "79", "81", "55", "94", "82", "100", "34", "50", "46", "91", "47" ]
for(var i = 0; i < arr.length; i++){
~~arr[i]
}
for(var i = 0; i < arr.length; i++){
Math.floor(Number(arr[i]))
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
tilde bit | |
Number |
Test name | Executions per second |
---|---|
tilde bit | 15352.6 Ops/sec |
Number | 5857.7 Ops/sec |
Measuring the performance of different approaches to achieve the same result is an essential aspect of optimizing code and ensuring that it runs efficiently.
Overview
The provided benchmark data compares two approaches for converting strings to integers:
Number()
. This is a commonly used method in languages that don't support Unicode or have issues with non-ASCII characters.Number()
and then applies Math.floor()
to get the integer value.Performance Comparison
The latest benchmark results show that:
Why does the TILDE BIT approach perform better?
There are several reasons why the TILDE BIT approach might be faster:
Number()
is also relatively fast, as it involves a simple arithmetic operation.Math.floor(Number())
.Recommendations
Based on the benchmark results, if performance is critical for this specific use case:
However, if simplicity and readability are more important than performance:
Keep in mind that benchmarking results can vary depending on specific hardware, software configurations, and environments.