Node.js 应用程序的优化,利用来自 JSON 文件的信息。

huangapple go评论54阅读模式
英文:

optimization of node.js application using information from a json file

问题

在我的应用程序中,我想根据给定的值显示各种化学元素的状态。有一个包含有关元素的所有信息的JSON文件。

例如,将有一个输入字段,用户可以输入所需的温度,然后会获得一个显示元素状态的显示,以其实际状态着色。为此,我需要循环遍历JSON文件,然后将值与输入的值进行比较。比较后,该值将设置为数组中的相应位置,并发送到电子显示单元。

对于除温度之外的其他属性(例如密度),将使用非常类似的函数。

JSON文件不可更改,因为它是外部生成的。

目前,我已经实现了这个函数:

const assumedInputTemperature = 20.0;
const display = new Array(119);
const color_solid = 1;
const color_liquid = 2;
const color_gas = 3;
const color_undefined = 4;

for (const key in elements) {
    const value = elements[key];
    const meltingPoint = value.MeltingPoint;
    const boilingPoint = value.BoilingPoint;
    const number = parseInt(key);

    console.log(`${number}: ${meltingPoint} : ${boilingPoint}`);

    if (meltingPoint == "" && boilingPoint == "") {
        console.log("Unknown!!?");
        display[number] = color_undefined;
    } else {
        if (meltingPoint > assumedInputTemperature) {
            display[number] = color_solid;
        } else if (meltingPoint < assumedInputTemperature && boilingPoint > assumedInputTemperature) {
            display[number] = color_liquid;
        } else if (meltingPoint < assumedInputTemperature && boilingPoint < assumedInputTemperature) {
            display[number] = color_gas;
        } else {
            console.log("ERROR!");
        }
    }
    console.log(display[number]);
}

这是功能性的,但我想知道是否有更好的方法来执行这个任务。是否有关于更好实现的建议?

英文:

In my application, I would like to display the state of various chemical elements depending on a given value. There is a JSON file that contains all the information about the element

{
 &quot;1&quot;:{
   &quot;Symbol&quot;:&quot;H&quot;,
   &quot;MeltingPoint&quot;: 13.81,
   &quot;BoilingPoint&quot;: 20.28,
    ...
  }
}

For example, there will be an input field where the user can enter a desired temperature and then will get a display of the elements, colored by their actual state. For this, I need to loop through the JSON file and then compare the values to the entered value. After the comparison, the value will be set to a corresponding position in an array and sent to an electronic display unit.
For other properties than temperature (eg. density) a very similar function will be used
The JSON-File is not changeable because it's generated externally.

Currently, I implemented this function:

const assumedInputTemperature = 20.0;
const display = new Array(119);
const color_solid = 1;
const color_liquid = 2;
const color_gas = 3;
const color_undefinded = 4;

    for (const key in elements){

    const value = elements[key];
    const meltingPoint = value.MeltingPoint;
    const boilingPoint = value.BoilingPoint1;
    const number = parseInt(key)

    console.log(`${number}: ${meltingPoint} : ${boilingPoint}`)

    if(meltingPoint == &quot;&quot; &amp;&amp; boilingPoint == &quot;&quot;){
        console.log(&quot;Unknown!!?&quot;)
        display[number] = color_undefinded;
    } else {
        if (meltingPoint &gt; assumedInputTemperature) {
            display[number] = color_solid;
        } else if (meltingPoint &lt; assumedInputTemperature &amp;&amp; boilingPoint &gt; assumedInputTemperature) {
            display[number] = color_liquid;
        } else if (meltingPoint &lt; assumedInputTemperature &amp;&amp; boilingPoint &lt; assumedInputTemperature) {
            display[number] = color_gas;
        } else {
            console.log(&quot;ERROR!&quot;);
        }
    }
    console.log(display[number]);
    }

This is functional, but I wonder if there is a better approach to doing this. Are there any
recommendations for a better implementation?

答案1

得分: 0

你可以使用Map数据结构,以其中一个温度值作为键,然后通过键来查找项目。

我的建议属于“花费设置时间和内存来组织数据结构以实现快速访问”的范畴。

但你可以假设在你的代码寿命内,周期表中的元素少于150个(目前已知有118个元素)。因此,你现在所使用的逐个查找每个项目的方法完全足够。没有必要为了更快地搜索特定的数据集而引入复杂性或测试时间。

但是,如果你的应用程序旨在搜索许多有机合成前体化合物,你现在的方法就不足够了。在你的数据集扩大的情况下,你应考虑使用数据库。许多科学应用程序使用简单且免费的SQLite数据库。

英文:

You could use Map data structures with one of the temperature values as the key, then look up the items by key.

My suggestion falls under the heading "spend setup time and memory organizing your data structure for fast access".

But you can probably assume that there are less than 150 elements in the periodic table for the lifetime of your code (118 elements are known now). So the look-at-each-item search you have now is perfectly adequate. It's not worth incurring complexity or test time to search that particular data set faster.

BUT, if your app is intended to search, I dunno, many organic-synthesis precursor compounds, what you have is not adequate. In the case where your dataset scales way up you should consider using a database. Many scientific apps use the simple and free SQLite database.

答案2

得分: 0

这是您提供的代码的中文翻译:

不知道您希望通过更好地完成它来实现什么目标您的代码可读性高易于理解这已经足够不错了但是下面是我如何做的示例

const display = [];
const color_solid = 1;
const color_liquid = 2;
const color_gas = 3;
const color_undefined = 4;

const assumedInputTemperature = 20.0;

const getColor = (meltingPoint, boilingPoint) => {
    if (isNaN(meltingPoint)) {
        // 熔点不是数字,但第一个检查需要
        // 或者您应该将其与上面的IF检查合并。我不太清楚
        console.log("未知!!?")
        return color_undefined
    }

    if (meltingPoint > assumedInputTemperature) return color_solid

    if (isNaN(boilingPoint)) {
        // 沸点不是数字,但第二个检查需要
        console.log("未知!!?")
        return color_undefined
    }

    // 这里我们已经知道熔点 <= assumedInputTemperature,无需检查
    if (boilingPoint > assumedInputTemperature) return color_liquid
    // 这里我们知道沸点 <= assumedInputTemperature

    return color_gas
}

for (const key in elements) {

    const value = elements[key];
    const meltingPoint = value.MeltingPoint;
    const boilingPoint = value.BoilingPoint1;
    const number = parseInt(key)

    console.log(`${number}: ${meltingPoint} : ${boilingPoint}`)

    display[number] = getColor(meltingPoint, boilingPoint)

    console.log(display[number]);
}

希望这个翻译对您有所帮助。如果您需要进一步的协助,请随时告诉我。

英文:

Don't know exactly you want to achieve by doing it better. Your code is readable and easy understand which is good enough.
But here is example how I would do it:

const display = [];
const color_solid = 1;
const color_liquid = 2;
const color_gas = 3;
const color_undefinded = 4;

const assumedInputTemperature = 20.0;

const getColor = (meltingPoint, boilingPoint) =&gt; {
    if(isNaN(meltingPoint)){
        // meltingPoint is not number but needed for first check
        // or you should combine it with upper IF check. It is unclear to me
        console.log(&quot;Unknown!!?&quot;)
        return color_undefinded
    }

    if(meltingPoint &gt; assumedInputTemperature) return  color_solid

    if(isNaN(boilingPoint)) {
        // boilingPoint is not a number but needed for second check
        console.log(&quot;Unknown!!?&quot;)
        return color_undefinded
    }

    // here we already know meltingPoint &lt;= assumedInputTemperature noo need to check
    if(boilingPoint &gt; assumedInputTemperature) return color_liquid
    // here we know boilingPoint &lt;= assumedInputTemperature

    return color_gas
}

for (const key in elements){

    const value = elements[key];
    const meltingPoint = value.MeltingPoint;
    const boilingPoint = value.BoilingPoint1;
    const number = parseInt(key)

    console.log(`${number}: ${meltingPoint} : ${boilingPoint}`)

    display[number] = getColor(meltingPoint, boilingPoint)

    console.log(display[number]);
}

huangapple
  • 本文由 发表于 2023年5月13日 19:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定