Check if two files are identical using Javascript, Typescript, or Php

Comparing two files with a programming language may be a bit tricky when the files are big, cause we may need a lot of time or processing power to check if the data is exactly the same. Normally, the best (fastest) way to approach this situation is to use a hash for each one of the files and compare them. If hash or file sizes are different, we are sure that the files are different. If hash and file sizes are the same, we may want to perform a full file comparison to get 100% confidence of the result. Unfortunately, having a file hash available is not a common scenario, so the only generic way to compare files is by performing a one by one comparison till a difference is found. If we need to compute a hash to compare both files there is no performance or time gain as we will need to read both two files full. The TurboDepot library FilesManager class has a method that does this comparison for us with the fastest algorithm possible. Let's see how we can compare any files we want in javascript, typescript, or PHP:

Test with PHP if two files are the same

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\managers\FilesManager;

$filesManager = new FilesManager();
$filesManager->isFileEqualTo('path/to/file1', 'path/to/file2');

isFileEqualTo() will return true if both files are valid and contain the same data.

Test with Javascript (node js) if two files are the same

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.isFileEqualTo('path/to/file1', 'path/to/file2');

isFileEqualTo() will return true if both files are valid and contain the same data.

Test with Typescript (TS) if two files are the same

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.isFileEqualTo('path/to/file1', 'path/to/file2');

isFileEqualTo() will return true if both files are valid and contain the same data.