Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sudoku2
(version: 0)
Comparing performance of:
shao vs thre
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [[".",".",".",".","8",".",".",".","."], [".",".",".",".",".",".","5",".","."], [".",".",".",".","4",".",".","2","."], [".",".",".","3",".","9",".",".","."], [".",".","1","8",".",".","9",".","."], [".",".",".",".",".","5","1",".","."], [".",".","3",".",".","8",".",".","."], [".","1","2",".","3",".",".",".","."], [".",".",".",".",".","7",".",".","1"]]
Tests:
shao
function sudoku2(grid) { var empty = "."; var chunk = []; for (var i = 0; i < grid.length; i++) { for (var j = 0; j < grid.length; j++) { if (j % 3 === 0 && !checkChunk(i, j)) { return false; } if (checkRowValid(grid[i], grid[i][j], j)) { return false; } if (checkColumnValid(grid, i, j)) { return false; } } } return true; function checkChunk(rowIndex, colIndex) { var counter = rowIndex % 3; while (counter < 3 && (rowIndex + counter) < grid.length) { chunk.push(grid[rowIndex + counter].slice(colIndex, colIndex + 3)); counter++; } chunk = [].concat.apply([], chunk); var isChunkValid = chunk.every(function (item, i) { if (item === empty) { return true; } return chunk.indexOf(item, i + 1) <= 1; }); chunk = []; return isChunkValid; } function checkRowValid(row, item, rowIndex) { return item !== empty && row.indexOf(item, rowIndex + 1) !== -1; } function checkColumnValid(grid, rowIndex, colIndex) { var entries = 0; for (var row = 0; row < grid.length; row++) { if (grid[row][colIndex] !== empty && grid[rowIndex][colIndex] === grid[row][colIndex]) { entries++; } } return entries > 1; } } sudoku2(data);
thre
function sudoku2(grid) { for(var x = 0; x < grid.length; x++) { var checkRow = {}; var checkColumn = {}; var checkSquare = {}; var offsetX = Math.floor(x/3) * 3; var offsetY = (x % 3) * 3; for(var y = 0; y < grid.length; y++) { var squareX = Math.floor(y/3) + offsetX; var squareY = (y % 3) + offsetY; if (isBroken(checkRow, grid[x][y]) || isBroken(checkColumn, grid[y][x]) || isBroken(checkSquare, grid[squareX][squareY])) { return false; } } } function isBroken(check, value) { return (check[value] && value !== '.') || !(check[value] = true); } return true; }; sudoku2(data);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
shao
thre
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):
I'll do my best to break down the provided benchmark and its results. **Benchmark Overview** The provided benchmark measures the performance of JavaScript code in solving Sudoku puzzles using two different approaches: `sudoku2` and `thre`. The benchmark uses a predefined JSON data for the puzzle and compares the execution times of the two functions on different browsers and devices. **Options Being Compared** The two options being compared are: 1. **Sudoku2**: This function uses a chunk-based approach to check row, column, and square validity. It also checks if there's an empty value in the grid. 2. **Thre**: This function uses a simpler approach by checking each row, column, and square individually using a separate object (`checkRow`, `checkColumn`, and `checkSquare`) for validation. **Pros and Cons** * **Sudoku2**: + Pros: Efficient use of memory (chunk-based approach) and effective validation logic. + Cons: May have higher overhead due to the chunking mechanism. * **Thre**: + Pros: Simple and lightweight code with minimal memory usage. + Cons: More complex validation logic, which may lead to performance issues. **Library and Purpose** None of the provided functions explicitly use any external libraries. However, they both rely on built-in JavaScript features like arrays, loops, and conditional statements. **Special JS Features or Syntax** The benchmark uses the `every()` method, which is a modern JavaScript feature introduced in ECMAScript 2010 (ES5.1). It's used to check if all elements in an array pass a test. **Alternatives** Other approaches that could be considered for solving Sudoku puzzles include: * Using a dedicated Sudoku library or algorithm * Implementing dynamic programming techniques * Utilizing GPU acceleration for matrix operations However, the provided benchmark focuses on comparing JavaScript performance, so alternative implementations might not be directly relevant to this specific use case. Overall, the benchmark provides valuable insights into the performance characteristics of two different Sudoku solving approaches in JavaScript.
Related benchmarks:
using .length within and out of for loop
sudoku21
Table appending
comparing coercion string vs number
Comments
Confirm delete:
Do you really want to delete benchmark?