node-check-word icon indicating copy to clipboard operation
node-check-word copied to clipboard

Improvement suggestion

Open hieunc229 opened this issue 3 years ago • 2 comments

Hi David, interesting library.

I was using a word generator with check-word. It ran a few thousand words and the NPM version couldn't handle it. I noticed that the NPM version isn't up-to-date, and the current one seems to perform much better.

I made some changes that run ~4x faster with the version on Github. It first loads the txt word list file from the old library (ie words/en.txt), then puts them into a map (faster to check if a key exists)

There is a bit of change, including the word list files, so I didn't make a PR. Though I'm happy to if you plan to use it

Here is the code in Typescript

import fs from "fs";

const possibleLanguages = ['de', 'en', 'es', 'fr'];

export default function checkword(language: string) {

    language = language && language.toLowerCase() || 'en';

    if (possibleLanguages.indexOf(language) == -1) {
        throw new Error(language + " is not valid language");
    }

    const content = fs.readFileSync(__dirname + '/words/' + language + '.txt', { encoding: "utf8" });
    const words: { [name: string]: boolean } = {};
    
    content.split(/\n/).forEach(k => {
        words[k] = true;
    });

    return {
        check: function (word: string) {

            if (word in words) {
                return true
            }

            return false;
        }
    };
};

hieunc229 avatar Sep 03 '21 13:09 hieunc229

I realize this post is old but...

Couldn't you have just:

check: function (word: string) { return (word in words) }

m-e-conroy avatar Oct 11 '23 13:10 m-e-conroy

Definitely. What was I thinking!

hieunc229 avatar Oct 11 '23 15:10 hieunc229