UNPKG

console-performance

Version:

Simple performance.now shim with automatic logging

144 lines (123 loc) 3.73 kB
<!doctype html> <html> <head> <title>Performance example</title> </head> <body> <p>Check the console</p> <script src="../console-performance.js"></script> <script> /** Test case from http://jsperf.com/built-in-vs-js-sort/3 */ var TEST_ARRAY_SIZE = 10000; function createInput () { var input = []; for (var i = 0; i < TEST_ARRAY_SIZE; i += 1) { input.push(Math.floor(Math.random() * TEST_ARRAY_SIZE)); } return input; } var input = createInput(); function compare (a, b) { return (a < b) ? -1 : ((a > b) ? 1 : 0); } function swap (array, a, b) { var temp = array[a]; array[a] = array[b]; array[b] = temp; } function quickSort (array, comparator, start, end) { var lowSwap = start; var highSwap = end - 1; var pivot = start; while (pivot < highSwap && 1 > comparator(array[pivot], array[pivot + 1])) { pivot += 1; } if (pivot === highSwap) { return; } var pivotValue = array[pivot + 1]; while (lowSwap < highSwap) { while (lowSwap < highSwap && 1 > comparator(array[lowSwap], pivotValue)) { lowSwap += 1; } while (lowSwap < highSwap && 0 > comparator(pivotValue, array[highSwap])) { highSwap -= 1; } swap(array, lowSwap, highSwap); } quickSort(array, comparator, start, lowSwap); quickSort(array, comparator, lowSwap, end); } function quickSortNonRecursive(arr) { var stack = []; var left = 0; var right = arr.length - 1; var i, j, swap, temp; while(true) { if(right - left <= 25){ for(j=left+1; j<=right; j++) { swap = arr[j]; i = j-1; while(i >= left && arr[i] > swap) { arr[i+1] = arr[i--]; } arr[i+1] = swap; } if(stack.length == 0) break; right = stack.pop(); left = stack.pop(); } else { var median = (left + right) >> 1; i = left + 1; j = right; swap = arr[median]; arr[median] = arr[i]; arr[i] = swap; if(arr[left] > arr[right]) { swap = arr[left]; arr[left] = arr[right]; arr[right] = swap; } if(arr[i] > arr[right]) { swap = arr[i]; arr[i] = arr[right]; arr[right] = swap; } if(arr[left] > arr[i]) { swap = arr[left]; arr[left] = arr[i]; arr[i] = swap; } temp = arr[i]; while(true){ do i++; while(arr[i] < temp); do j--; while(arr[j] > temp); if(j < i) break; swap = arr[i]; arr[i] = arr[j]; arr[j] = swap; } arr[left + 1] = arr[j]; arr[j] = temp; if(right - i + 1 >= j - left){ stack.push(i); stack.push(right); right = j - 1; }else{ stack.push(left); stack.push(j - 1); left = i; } } } return arr; }; </script> <script> consolePerformance.start('builtInSort', 'Starting test "%s"'); input.sort(); consolePerformance.end('builtInSort', 'Test %s ended in %dms'); consolePerformance.start('builtInSortCompare', 'Starting test "%s"'); input.sort(compare); consolePerformance.end('builtInSortCompare', 'Test "%s" ended in %dms'); consolePerformance.start('quickSort', 'Starting test "%s"', true); quickSort(input, compare, 0, TEST_ARRAY_SIZE); consolePerformance.end('quickSort', 'Test "%s" ended in %dms', true); consolePerformance.start('quickSortNonRecursive', 'Starting test "%s"'); quickSortNonRecursive(input) consolePerformance.end('quickSortNonRecursive', 'Test "%s" ended in %dms'); consolePerformance.start('internalSort'); input.sort(function (a, b) { return a - b; } ); consolePerformance.end('internalSort'); </script> </body> </html>