Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#1423 closed Bug (invalid)

Possible Optimisation in fckeditorapi.js

Reported by: Scott McNaught Owned by:
Priority: Normal Milestone:
Component: General Version:
Keywords: Cc:

Description

I noticed today that there is a large string of javascript code which is generated and eval'd.

I was wondering if you could rather than eval'ing the script in the parent windows context, just do something like this?

parent.FCKeditorAPI = {
	Version : "[Development]",
	VersionBuild : "[DEV]",
	__Instances : new Object(),

	GetInstance : function( name )
	{
		return this.__Instances[ name ];
	}
...
};

I can see a performance optimisation for the reasons that:

  • You wont have to concatenate that sScript string over and over again.
  • Eval'd code in any language never runs as fast

I am however unaware if it is possible to set vars in the parent objects context like this in all browsers.

Change History (3)

comment:1 Changed 17 years ago by Frederico Caldeira Knabben

Resolution: invalid
Status: newclosed

The comment you will find there in the code is not that clear, but it tries to explain the problem we have here:

// Make the FCKeditorAPI object available in the parent window. Use
// eval so this core runs in the parent's scope and so it will still be
// available if the editor instance is removed ("Can't execute code
// from a freed script" error).

The problem is here:

GetInstance : function( name )
{
	return this.__Instances[ name ];
}

You are assigning the GetInstance property to a local anonymous function. In other words, GetInstance is simply a pointer to a anonymous variable in the running scope.

We may have many editor instances in the same page, but we want to have FCKeditorAPI initialized just once, and always available after that, even if all instances have been removed from the page.

When loading an editor instance, the "local main scope" is the window that holds the editor instance itself. So, if we dynamically create an editor instance, if it is the first one, it will create parent.FCKeditorAPI. Then, if we remove the instance iframe, all references to anonymous functions in FCKeditorAPI will get lost, and any attempt to use them will give errors ("Can't execute code from a freed script" in IE).

So, the only way to create the anonymous functions in the parent window is by calling eval in that window.

comment:2 Changed 17 years ago by Frederico Caldeira Knabben

Another note...

You wont have to concatenate that sScript string over and over again.

It was appositely written in that way for readability. This is not a big issue because the Packager will transform things like:

var myStr = "Part1," + "Part2," + "Part3" ;

... in:

var myStr = "Part1,Part2,Part3" ;

If you take a look at the compressed files, you will note that we have few long strings there, instead of many concatenations.

comment:3 Changed 17 years ago by Scott McNaught

Cheers, thanks for the explanation.

Note: See TracTickets for help on using tickets.
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy