Skip to main content

One post tagged with "node.js"

View All Tags

· 3 min read
Vikram

Overview

I recently worked on a small code snippet for reading a list of files from a folder. Each file has a unique ID and may reference another file within it.

The goal of the code is to find a file using its unique ID, search through all folders and subfolders to locate it, and then determine how many files are included within it. It's also possible that files included in the first file have links to other files.

There's no limit on the number of levels or files it can search through. Sometimes the same file ID may appear multiple times, so the code removes duplicates and presents the list as a JSON in hierarchical form.

Steps in Functional Flow

  • Input the file ID.
  • Start reading files from the main folder.
  • Skip files if their extension doesn't match.
  • If the file isn't found, keep searching subfolders recursively until it's found.
  • If no file is found, stop and display an error message.
  • If a file is found, stop searching further.
  • Treat this file as the rootFile.
  • Read all files included in the rootFile.
  • Check if included files have further files included in a loop.
  • If more files are found, search for the next level of included files recursively and store them in a JSON with two keys: id (String) and children (Array).
  • Stop searching if a file has no further files included.
  • Display the final result.

I initially wrote the code snippet in Python, but as most of my team members are beginners and still learning, I converted the logic to Node.js. I tried to write equivalent JavaScript for most of the code, line by line.

Benchmarks

Here are the results when I ran the Python and Node.js code for the same purpose, on the same machine, and for the same requirement.

                Node.js                 Python (MM:SS.MS)
----------------------------------------------------------
Product Env 90.731 seconds 02:17.111494
Cust I Env 120.099 seconds 02:57.662355
Cust II Env 80.626 seconds 01:57.616320

Machine Configuration

The code was run locally on my laptop, which has the following specs:

  • Model:-- Macbook Pro 2018 model
  • Processor:-- Intel chipset
  • RAM:-- 16 GB

Conclusion

For this specific use case, running the code locally as a single user, even if it takes 5 minutes, doesn't make much of a difference. Therefore, the speed of one technology over another isn't crucial here. This documentation is only for informational purposes and not to compare Node.js and Python.

Every programming language has its strengths and weaknesses. What works well in one scenario might not in another. Factors like concurrent executions, network speed, and server reliability play significant roles. Before deciding on a technology, thorough research and analysis are essential.