<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Best performance practice while using modules]]></title><description><![CDATA[<p>There is a question about what the best performance practice to use modules.</p>
<p>In the case we use module to determine creep's role behavior, for example, which variant will be more CPU-effective:</p>
<p><h3>Module as function (from documentation):</h3></p>
<pre><code class="javascript">/// harvester.js
module.exports = function(creep) {
    /// Some private vars
    /// Some supporting functions
    return {
        work : function() { /* JOB */ }
    }
}

/// main.js
var harvester = require('harvester');
for(var i in Game.creeps) {
    harvester(Game.creeps[i]).work();
}
</code></pre>
<p><h3>... or module as a result of function call:</h3></p>
<pre><code class="javascript">/// harvester.js
module.exports = (function() {
    /// Some private vars
    /// Some supporting functions
    return {
        work : function(creep) { /* JOB */ }
    }
}());

/// main.js
var harvester = require('harvester');
for(var i in Game.creeps) {
    harvester.work(Game.creeps[i]);
}
</code></pre>
<p>... or any other way?</p>
<p>Have anybody thought about this and mb did any cpu-usage measures?</p>]]></description><link>http://screeps.com/forum/topic/1364/best-performance-practice-while-using-modules</link><generator>RSS for Node</generator><lastBuildDate>Mon, 16 Mar 2026 18:37:27 GMT</lastBuildDate><atom:link href="http://screeps.com/forum/topic/1364.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 Sep 2015 23:22:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Best performance practice while using modules on Invalid Date]]></title><description><![CDATA[<p>There is a question about what the best performance practice to use modules.</p>
<p>In the case we use module to determine creep's role behavior, for example, which variant will be more CPU-effective:</p>
<p><h3>Module as function (from documentation):</h3></p>
<pre><code class="javascript">/// harvester.js
module.exports = function(creep) {
    /// Some private vars
    /// Some supporting functions
    return {
        work : function() { /* JOB */ }
    }
}

/// main.js
var harvester = require('harvester');
for(var i in Game.creeps) {
    harvester(Game.creeps[i]).work();
}
</code></pre>
<p><h3>... or module as a result of function call:</h3></p>
<pre><code class="javascript">/// harvester.js
module.exports = (function() {
    /// Some private vars
    /// Some supporting functions
    return {
        work : function(creep) { /* JOB */ }
    }
}());

/// main.js
var harvester = require('harvester');
for(var i in Game.creeps) {
    harvester.work(Game.creeps[i]);
}
</code></pre>
<p>... or any other way?</p>
<p>Have anybody thought about this and mb did any cpu-usage measures?</p>]]></description><link>http://screeps.com/forum/post/6116</link><guid isPermaLink="true">http://screeps.com/forum/post/6116</guid><dc:creator><![CDATA[Mototroller]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Best performance practice while using modules on Invalid Date]]></title><description><![CDATA[<p>In terms of "CPU performance": <strong>each</strong> <code>require</code> call takes 0.6 cpu on the average, no matter how you use it's result further.<br>
(that is according to my own measures)</p>
<p>So, if you're interested in a high performance, you may think about minimising the number of modules (the number of <code>require</code> calls in your script) rather then it's further usage.</p>
<p>About your initial question: I don't see any evident difference in it (due to measures I did), but there may be other opinions.</p>]]></description><link>http://screeps.com/forum/post/6117</link><guid isPermaLink="true">http://screeps.com/forum/post/6117</guid><dc:creator><![CDATA[neomatrix]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Best performance practice while using modules on Invalid Date]]></title><description><![CDATA[<p>Хорошо, спасибо.</p>
<p>P.S. Увидел в соседней ветке подробную расписку по замерам <img
      src="http://screeps.com/forum/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=a1k070tfs06"
      class="not-responsive emoji emoji-android emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>]]></description><link>http://screeps.com/forum/post/6118</link><guid isPermaLink="true">http://screeps.com/forum/post/6118</guid><dc:creator><![CDATA[Mototroller]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Best performance practice while using modules on Invalid Date]]></title><description><![CDATA[<p>Interesting, so there is a direct relationship between maintainability and cost (assuming code is more maintainable when divided into modules). For you to decide if its worth it! </p>]]></description><link>http://screeps.com/forum/post/6119</link><guid isPermaLink="true">http://screeps.com/forum/post/6119</guid><dc:creator><![CDATA[OkkeHendriks]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Best performance practice while using modules on Invalid Date]]></title><description><![CDATA[<p>I've found that each require call takes an amount of time correlated to how much the file contains...  Following neomatrix's testing I created a bundle containing all my code then measured it's require time in main.js... 23 ms.</p>]]></description><link>http://screeps.com/forum/post/6120</link><guid isPermaLink="true">http://screeps.com/forum/post/6120</guid><dc:creator><![CDATA[Hatyr]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>