Thanks to the AI, I updated to JXA code in a lot of my km-macros. Even though I know nothing about JavaScript coding.
But I always feel the JXA is slower in KM.
I just find this example.
in the past, I use km action method to do this - Check if an item is in an Array. I loop all items in this array, if meet, return YES. if no, return empty. the macro is like:
After AI, i made a completely new one, the JXA code in it is like:
// Set up application
var app = Application.currentApplication();
app.includeStandardAdditions = true;
function checkValueInArray(arrayA, inputValue) {
/**
* 检查值是否存在于数组中
* @param {Array} arrayA - 输入数组
* @param {*} inputValue - 要检查的值
* @returns {String} - 如果找到返回"YES",否则返回""
*/
// 将输入字符串转换为数组
let array = String(arrayA).split(',').map(function(item) {
item = item.trim(); // 移除空格
// 如果是数字字符串,转换为数字
return !isNaN(item) ? Number(item) : item;
});
// 将输入值转换为数字(如果是数字字符串)
inputValue = !isNaN(inputValue) ? Number(inputValue) : inputValue;
// 检查值是否存在于数组中
return array.includes(inputValue) ? "YES" : "";
}
// 获取KM变量
var inputArray = kmvar.Local__ARRAY;
var checkValue = kmvar.Local__InputValue;
// 调用函数并获取结果
var result = checkValueInArray(inputArray, checkValue);
// 设置KM变量
var kme = Application("Keyboard Maestro Engine");
kme.setvariable("finalOutputJavaScript", { to: result });
And I use this method to test the running time.
the test result on my machine is:
km-method take 0.03s average.
js-method take 0.17s average.
A simple one-off JXA will generally be slower than a "normal" KM action because of the time taken to instantiate the JXA environment.
So a KM macro that works through a list of 100 items and passes each item in turn to a JXA action will be a lot slower than a KM macro that passes the entire list to the JXA which then works through each item in turn -- 100 separate instantiations versus one.
In addition, the "AI" (artificial language) JS there is full of unnecessary baggage.
Fine to use JS if you really want to learn it, but the artificial language (LLM) pathway will just waste your time. The LLM has learned the syntax of JS, but doesn't understand:
shown us the format of your CSV rows (are the strings double quoted, for example ?)
explained what problem you are trying to solve
why are you checking whether a value is in a row ?
are you running multiple checks of this kind ?
etc.
If you are just checking for a value between commas in unquoted CSV, you could just search for comma-delimited versions of the target value, or use as little JS as:
const v = kmvar.local_Value;
return kmvar.local_CSV.split(",").includes(v);
but to be honest, if you don't feel curious about experimenting with JS, and learning a bit of it yourself, then much better to avoid it completely.
The "artificial language" (LLM) output is really not serious or reliable – it's a kind of parody.
For example, at the top of the LLM pastiche that you were given, we see:
// Set up application
var app = Application.currentApplication();
app.includeStandardAdditions = true;
but set up what application ?
Where is that app refererence, or the inclusion of the Standard Additions library, actually used ?
Nowhere. The artificial language system hasn't a clue – it's just sticking together random things that it has found. It has no sense of their meaning whatsoever.
the test result is 0.05s average. much faster, but still slower than km-action method (0.03s). But that's a good thing.
Since I already did a lot with help of AI, According to @Nige_S , if the workflow is wrong, most of these macros need to go back to km-action structure. But if optimizing code can make big difference, I 'll check this first.
The task I am currently doing requires processing a large number of arrays. My macro is a subroutine. It was called in multiple-level-looping-structure. This subroutine can be called over twenty or thirty times in a run.
the looped part is like:
‘’‘
Do A in KM action
+
Do B in JXA in subroutine
+
Do C in KM action
’‘’
I thought it would be more time efficient to use js to process a lot of arrays. But according to @Nige_S , I need to rethink about this.
For example, if I need to all go to AppleScript method. Since I never feel delay when use AS-action-structure-macro. Although using AS to implement the same functionality requires longer code.
"Append to" appears to risk creating strings like falsefalsetruetruefalse
appending one more string to the end each time, and always failing the the is true test.
Perhaps you need to adjust the option:
from Append to variable:
to Set variable
?
Another thing to experiment with (if you are interested in performance – it's not something that I personally measure or spend human time on in Keyboard Maestro) you might find that limiting the variables exported to JXA (in the menu behind the small chevron to the left of the code text field (to your Local__InputValue and Local__Array) can improve performance, if you have a large number Keyboard Maestro variables, or any very big variable values.
(I notice that, at the moment, the setting you have is the default "Include All Variables", and it's conceivable that that adds a small overhead. Try just checking the names of the two variables that you are actually using in your JS)
Again -- instantiation time. If you haven't seen it, look at this post I did with timings.
If JXA/AS/shell script can provide functionality that KM doesn't have, use a script. If a single JXA/AS/shell action can replace many KM actions (which includes a loop structure with many iterations), use a script. But don't replace simple KM actions with simple JXA/AS/shell actions to try and increase performance, it rarely works.